Skip to content

Commit c4ad07e

Browse files
committed
AST: add enum FunctionTypeRepresentation and TypeProperties.functionTypeRepresentation
1 parent 73ce0af commit c4ad07e

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ extension TypeProperties {
201201
GenericSignature(bridged: rawType.bridged.getInvocationGenericSignatureOfFunctionType())
202202
}
203203

204+
public var functionTypeRepresentation: FunctionTypeRepresentation {
205+
switch rawType.bridged.getFunctionTypeRepresentation() {
206+
case .Thick: return .thick
207+
case .Block: return .block
208+
case .Thin: return .thin
209+
case .CFunctionPointer: return .cFunctionPointer
210+
case .Method: return .method
211+
case .ObjCMethod: return .objCMethod
212+
case .WitnessMethod: return .witnessMethod
213+
case .Closure: return .closure
214+
case .CXXMethod: return .cxxMethod
215+
case .KeyPathAccessorGetter: return .keyPathAccessorGetter
216+
case .KeyPathAccessorSetter: return .keyPathAccessorSetter
217+
case .KeyPathAccessorEquals: return .keyPathAccessorEquals
218+
case .KeyPathAccessorHash: return .keyPathAccessorHash
219+
default: fatalError()
220+
}
221+
}
222+
204223
//===--------------------------------------------------------------------===//
205224
// Type properties
206225
//===--------------------------------------------------------------------===//
@@ -275,6 +294,59 @@ extension TypeProperties {
275294
}
276295
}
277296

297+
public enum FunctionTypeRepresentation {
298+
/// A freestanding thick function.
299+
case thick
300+
301+
/// A thick function that is represented as an Objective-C block.
302+
case block
303+
304+
/// A freestanding thin function that needs no context.
305+
case thin
306+
307+
/// A C function pointer, which is thin and also uses the C calling convention.
308+
case cFunctionPointer
309+
310+
/// A Swift instance method.
311+
case method
312+
313+
/// An Objective-C method.
314+
case objCMethod
315+
316+
/// A Swift protocol witness.
317+
case witnessMethod
318+
319+
/// A closure invocation function that has not been bound to a context.
320+
case closure
321+
322+
/// A C++ method that takes a "this" argument (not a static C++ method or constructor).
323+
/// Except for handling the "this" argument, has the same behavior as "CFunctionPointer".
324+
case cxxMethod
325+
326+
/// A KeyPath accessor function, which is thin and also uses the variadic length generic
327+
/// components serialization in trailing buffer. Each representation has a different convention
328+
/// for which parameters have serialized generic type info.
329+
case keyPathAccessorGetter, keyPathAccessorSetter, keyPathAccessorEquals, keyPathAccessorHash
330+
331+
public var bridged: BridgedASTType.FunctionTypeRepresentation {
332+
switch self {
333+
case .thick: return .Thick
334+
case .block: return .Block
335+
case .thin: return .Thin
336+
case .cFunctionPointer: return .CFunctionPointer
337+
case .method: return .Method
338+
case .objCMethod: return .ObjCMethod
339+
case .witnessMethod: return .WitnessMethod
340+
case .closure: return .Closure
341+
case .cxxMethod: return .CXXMethod
342+
case .keyPathAccessorGetter: return .KeyPathAccessorGetter
343+
case .keyPathAccessorSetter: return .KeyPathAccessorSetter
344+
case .keyPathAccessorEquals: return .KeyPathAccessorEquals
345+
case .keyPathAccessorHash: return .KeyPathAccessorHash
346+
}
347+
}
348+
}
349+
278350
public struct TypeArray : RandomAccessCollection, CustomReflectable {
279351
public let bridged: BridgedASTTypeArray
280352

include/swift/AST/ASTBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,22 @@ struct BridgedASTType {
29362936
ObjC
29372937
};
29382938

2939+
enum class FunctionTypeRepresentation {
2940+
Thick = 0,
2941+
Block,
2942+
Thin,
2943+
CFunctionPointer,
2944+
Method = 8,
2945+
ObjCMethod,
2946+
WitnessMethod,
2947+
Closure,
2948+
CXXMethod,
2949+
KeyPathAccessorGetter,
2950+
KeyPathAccessorSetter,
2951+
KeyPathAccessorEquals,
2952+
KeyPathAccessorHash
2953+
};
2954+
29392955
swift::TypeBase * _Nullable type;
29402956

29412957
BRIDGED_INLINE swift::Type unbridged() const;
@@ -2998,6 +3014,7 @@ struct BridgedASTType {
29983014
BRIDGED_INLINE BridgedOptionalInt getValueOfIntegerType() const;
29993015
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap() const;
30003016
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
3017+
BRIDGED_INLINE FunctionTypeRepresentation getFunctionTypeRepresentation() const;
30013018
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30023019
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
30033020
BRIDGED_INLINE bool containsSILPackExpansionType() const;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,25 @@ BridgedGenericSignature BridgedASTType::getInvocationGenericSignatureOfFunctionT
644644
return {unbridged()->castTo<swift::SILFunctionType>()->getInvocationGenericSignature().getPointer()};
645645
}
646646

647+
BridgedASTType::FunctionTypeRepresentation BridgedASTType::getFunctionTypeRepresentation() const {
648+
static_assert((int)FunctionTypeRepresentation::Thick == (int)swift::SILFunctionTypeRepresentation::Thick);
649+
static_assert((int)FunctionTypeRepresentation::Block == (int)swift::SILFunctionTypeRepresentation::Block);
650+
static_assert((int)FunctionTypeRepresentation::Thin == (int)swift::SILFunctionTypeRepresentation::Thin);
651+
static_assert((int)FunctionTypeRepresentation::CFunctionPointer == (int)swift::SILFunctionTypeRepresentation::CFunctionPointer);
652+
static_assert((int)FunctionTypeRepresentation::Method == (int)swift::SILFunctionTypeRepresentation::Method);
653+
static_assert((int)FunctionTypeRepresentation::ObjCMethod == (int)swift::SILFunctionTypeRepresentation::ObjCMethod);
654+
static_assert((int)FunctionTypeRepresentation::WitnessMethod == (int)swift::SILFunctionTypeRepresentation::WitnessMethod);
655+
static_assert((int)FunctionTypeRepresentation::Closure == (int)swift::SILFunctionTypeRepresentation::Closure);
656+
static_assert((int)FunctionTypeRepresentation::CXXMethod == (int)swift::SILFunctionTypeRepresentation::CXXMethod);
657+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorGetter == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorGetter);
658+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorSetter == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorSetter);
659+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorEquals == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorEquals);
660+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorHash == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorHash);
661+
662+
auto fnType = unbridged()->castTo<swift::SILFunctionType>();
663+
return (FunctionTypeRepresentation)(fnType->getRepresentation());
664+
}
665+
647666
BridgedASTType BridgedASTType::subst(BridgedSubstitutionMap substMap) const {
648667
return {unbridged().subst(substMap.unbridged()).getPointer()};
649668
}

0 commit comments

Comments
 (0)