Skip to content

Commit 67925ea

Browse files
committed
Swift AST: add GenericSignature
and add `var Type.invocationGenericSignatureOfFunctionType`
1 parent 0f55220 commit 67925ea

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

SwiftCompilerSources/Sources/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_swift_compiler_module(AST
1212
SOURCES
1313
Declarations.swift
1414
Conformance.swift
15+
GenericSignature.swift
1516
Registration.swift
1617
SubstitutionMap.swift
1718
Type.swift)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===--- GenericSignature.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Basic
14+
import ASTBridging
15+
16+
/// Describes the generic signature of a particular declaration, including both the generic type
17+
/// parameters and the requirements placed on those generic parameters.
18+
public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
19+
public let bridged: BridgedGenericSignature
20+
21+
public init(bridged: BridgedGenericSignature) {
22+
self.bridged = bridged
23+
}
24+
25+
public var description: String {
26+
return String(taking: bridged.getDebugDescription())
27+
}
28+
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ extension TypeProperties {
9797
public var representationOfMetatype: AST.`Type`.MetatypeRepresentation {
9898
type.bridged.getRepresentationOfMetatype().representation
9999
}
100+
public var invocationGenericSignatureOfFunctionType: GenericSignature {
101+
GenericSignature(bridged: type.bridged.getInvocationGenericSignatureOfFunctionType())
102+
}
100103

101104
public var canBeClass: Type.TraitResult { type.bridged.canBeClass().result }
102105

include/swift/AST/ASTBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ enum class DifferentiabilityKind : uint8_t;
4949
class Fingerprint;
5050
class Identifier;
5151
class IfConfigClauseRangeInfo;
52+
class GenericSignature;
53+
class GenericSignatureImpl;
5254
struct LabeledStmtInfo;
5355
class LayoutConstraint;
5456
class LayoutConstraintInfo;
@@ -75,6 +77,7 @@ struct BridgedASTType;
7577
class BridgedCanType;
7678
class BridgedASTContext;
7779
struct BridgedSubstitutionMap;
80+
struct BridgedGenericSignature;
7881
struct BridgedConformance;
7982
class BridgedParameterList;
8083
enum BridgedPlatformKind : size_t;
@@ -3015,6 +3018,7 @@ struct BridgedASTType {
30153018
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getAnyNominal() const;
30163019
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getInstanceTypeOfMetatype() const;
30173020
BRIDGED_INLINE MetatypeRepresentation getRepresentationOfMetatype() const;
3021+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
30183022
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30193023
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedASTType fromType, BridgedASTType toType) const;
30203024
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
@@ -3078,6 +3082,13 @@ struct BridgedSubstitutionMap {
30783082
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getReplacementTypes() const;
30793083
};
30803084

3085+
struct BridgedGenericSignature {
3086+
const swift::GenericSignatureImpl * _Nullable impl;
3087+
3088+
BRIDGED_INLINE swift::GenericSignature unbridged() const;
3089+
BridgedOwnedString getDebugDescription() const;
3090+
};
3091+
30813092
struct BridgedFingerprint {
30823093
uint64_t v1;
30833094
uint64_t v2;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/ConformanceLookup.h"
2020
#include "swift/AST/Decl.h"
2121
#include "swift/AST/Expr.h"
22+
#include "swift/AST/GenericSignature.h"
2223
#include "swift/AST/IfConfigClauseRangeInfo.h"
2324
#include "swift/AST/MacroDeclaration.h"
2425
#include "swift/AST/ProtocolConformance.h"
@@ -453,6 +454,10 @@ BridgedASTType::MetatypeRepresentation BridgedASTType::getRepresentationOfMetaty
453454
return MetatypeRepresentation(unbridged()->getAs<swift::AnyMetatypeType>()->getRepresentation());
454455
}
455456

457+
BridgedGenericSignature BridgedASTType::getInvocationGenericSignatureOfFunctionType() const {
458+
return {unbridged()->castTo<swift::SILFunctionType>()->getInvocationGenericSignature().getPointer()};
459+
}
460+
456461
BridgedASTType BridgedASTType::subst(BridgedSubstitutionMap substMap) const {
457462
return {unbridged().subst(substMap.unbridged()).getPointer()};
458463
}
@@ -660,6 +665,14 @@ BridgedASTTypeArray BridgedSubstitutionMap::getReplacementTypes() const {
660665
return {unbridged().getReplacementTypes()};
661666
}
662667

668+
//===----------------------------------------------------------------------===//
669+
// MARK: BridgedGenericSignature
670+
//===----------------------------------------------------------------------===//
671+
672+
swift::GenericSignature BridgedGenericSignature::unbridged() const {
673+
return swift::GenericSignature(impl);
674+
}
675+
663676
//===----------------------------------------------------------------------===//
664677
// MARK: BridgedFingerprint
665678
//===----------------------------------------------------------------------===//

lib/AST/Bridging/MiscBridging.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,14 @@ BridgedOwnedString BridgedSubstitutionMap::getDebugDescription() const {
121121
unbridged().dump(os);
122122
return BridgedOwnedString(str);
123123
}
124+
125+
//===----------------------------------------------------------------------===//
126+
// MARK: GenericSignature
127+
//===----------------------------------------------------------------------===//
128+
129+
BridgedOwnedString BridgedGenericSignature::getDebugDescription() const {
130+
std::string str;
131+
llvm::raw_string_ostream os(str);
132+
unbridged().print(os);
133+
return BridgedOwnedString(str);
134+
}

0 commit comments

Comments
 (0)