Skip to content

Commit 4920ace

Browse files
committed
AST: add enum FunctionTypeRepresentation and TypeProperties.functionTypeRepresentation
1 parent 6b260a2 commit 4920ace

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
@@ -227,6 +227,25 @@ extension TypeProperties {
227227
GenericSignature(bridged: rawType.bridged.getInvocationGenericSignatureOfFunctionType())
228228
}
229229

230+
public var functionTypeRepresentation: FunctionTypeRepresentation {
231+
switch rawType.bridged.getFunctionTypeRepresentation() {
232+
case .Thick: return .thick
233+
case .Block: return .block
234+
case .Thin: return .thin
235+
case .CFunctionPointer: return .cFunctionPointer
236+
case .Method: return .method
237+
case .ObjCMethod: return .objCMethod
238+
case .WitnessMethod: return .witnessMethod
239+
case .Closure: return .closure
240+
case .CXXMethod: return .cxxMethod
241+
case .KeyPathAccessorGetter: return .keyPathAccessorGetter
242+
case .KeyPathAccessorSetter: return .keyPathAccessorSetter
243+
case .KeyPathAccessorEquals: return .keyPathAccessorEquals
244+
case .KeyPathAccessorHash: return .keyPathAccessorHash
245+
default: fatalError()
246+
}
247+
}
248+
230249
//===--------------------------------------------------------------------===//
231250
// Type properties
232251
//===--------------------------------------------------------------------===//
@@ -307,6 +326,59 @@ extension TypeProperties {
307326
}
308327
}
309328

329+
public enum FunctionTypeRepresentation {
330+
/// A freestanding thick function.
331+
case thick
332+
333+
/// A thick function that is represented as an Objective-C block.
334+
case block
335+
336+
/// A freestanding thin function that needs no context.
337+
case thin
338+
339+
/// A C function pointer, which is thin and also uses the C calling convention.
340+
case cFunctionPointer
341+
342+
/// A Swift instance method.
343+
case method
344+
345+
/// An Objective-C method.
346+
case objCMethod
347+
348+
/// A Swift protocol witness.
349+
case witnessMethod
350+
351+
/// A closure invocation function that has not been bound to a context.
352+
case closure
353+
354+
/// A C++ method that takes a "this" argument (not a static C++ method or constructor).
355+
/// Except for handling the "this" argument, has the same behavior as "CFunctionPointer".
356+
case cxxMethod
357+
358+
/// A KeyPath accessor function, which is thin and also uses the variadic length generic
359+
/// components serialization in trailing buffer. Each representation has a different convention
360+
/// for which parameters have serialized generic type info.
361+
case keyPathAccessorGetter, keyPathAccessorSetter, keyPathAccessorEquals, keyPathAccessorHash
362+
363+
public var bridged: BridgedASTType.FunctionTypeRepresentation {
364+
switch self {
365+
case .thick: return .Thick
366+
case .block: return .Block
367+
case .thin: return .Thin
368+
case .cFunctionPointer: return .CFunctionPointer
369+
case .method: return .Method
370+
case .objCMethod: return .ObjCMethod
371+
case .witnessMethod: return .WitnessMethod
372+
case .closure: return .Closure
373+
case .cxxMethod: return .CXXMethod
374+
case .keyPathAccessorGetter: return .KeyPathAccessorGetter
375+
case .keyPathAccessorSetter: return .KeyPathAccessorSetter
376+
case .keyPathAccessorEquals: return .KeyPathAccessorEquals
377+
case .keyPathAccessorHash: return .KeyPathAccessorHash
378+
}
379+
}
380+
}
381+
310382
public struct TypeArray : RandomAccessCollection, CustomReflectable {
311383
public let bridged: BridgedASTTypeArray
312384

include/swift/AST/ASTBridging.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,22 @@ struct BridgedASTType {
29852985
ObjC
29862986
};
29872987

2988+
enum class FunctionTypeRepresentation {
2989+
Thick = 0,
2990+
Block,
2991+
Thin,
2992+
CFunctionPointer,
2993+
Method = 8,
2994+
ObjCMethod,
2995+
WitnessMethod,
2996+
Closure,
2997+
CXXMethod,
2998+
KeyPathAccessorGetter,
2999+
KeyPathAccessorSetter,
3000+
KeyPathAccessorEquals,
3001+
KeyPathAccessorHash
3002+
};
3003+
29883004
swift::TypeBase * _Nullable type;
29893005

29903006
BRIDGED_INLINE swift::Type unbridged() const;
@@ -3047,6 +3063,7 @@ struct BridgedASTType {
30473063
BRIDGED_INLINE BridgedOptionalInt getValueOfIntegerType() const;
30483064
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap() const;
30493065
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
3066+
BRIDGED_INLINE FunctionTypeRepresentation getFunctionTypeRepresentation() const;
30503067
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30513068
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapOutOfEnvironment() const;
30523069
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType

include/swift/AST/ASTBridgingImpl.h

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

716+
BridgedASTType::FunctionTypeRepresentation BridgedASTType::getFunctionTypeRepresentation() const {
717+
static_assert((int)FunctionTypeRepresentation::Thick == (int)swift::SILFunctionTypeRepresentation::Thick);
718+
static_assert((int)FunctionTypeRepresentation::Block == (int)swift::SILFunctionTypeRepresentation::Block);
719+
static_assert((int)FunctionTypeRepresentation::Thin == (int)swift::SILFunctionTypeRepresentation::Thin);
720+
static_assert((int)FunctionTypeRepresentation::CFunctionPointer == (int)swift::SILFunctionTypeRepresentation::CFunctionPointer);
721+
static_assert((int)FunctionTypeRepresentation::Method == (int)swift::SILFunctionTypeRepresentation::Method);
722+
static_assert((int)FunctionTypeRepresentation::ObjCMethod == (int)swift::SILFunctionTypeRepresentation::ObjCMethod);
723+
static_assert((int)FunctionTypeRepresentation::WitnessMethod == (int)swift::SILFunctionTypeRepresentation::WitnessMethod);
724+
static_assert((int)FunctionTypeRepresentation::Closure == (int)swift::SILFunctionTypeRepresentation::Closure);
725+
static_assert((int)FunctionTypeRepresentation::CXXMethod == (int)swift::SILFunctionTypeRepresentation::CXXMethod);
726+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorGetter == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorGetter);
727+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorSetter == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorSetter);
728+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorEquals == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorEquals);
729+
static_assert((int)FunctionTypeRepresentation::KeyPathAccessorHash == (int)swift::SILFunctionTypeRepresentation::KeyPathAccessorHash);
730+
731+
auto fnType = unbridged()->castTo<swift::SILFunctionType>();
732+
return (FunctionTypeRepresentation)(fnType->getRepresentation());
733+
}
734+
716735
BridgedASTType BridgedASTType::subst(BridgedSubstitutionMap substMap) const {
717736
return {unbridged().subst(substMap.unbridged()).getPointer()};
718737
}

0 commit comments

Comments
 (0)