-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathDerivedConformanceKeyPathIterable.cpp
180 lines (161 loc) · 7.36 KB
/
DerivedConformanceKeyPathIterable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===--- DerivedConformanceKeyPathIterable.cpp ----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements explicit derivation of the KeyPathIterable protocol for
// a nominal type.
//
//===----------------------------------------------------------------------===//
#include "CodeSynthesis.h"
#include "TypeChecker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Types.h"
#include "DerivedConformances.h"
using namespace swift;
bool DerivedConformance::canDeriveKeyPathIterable(NominalTypeDecl *nominal) {
// Note: we could extend synthesis to support classes.
// Subclasses need to append `allKeyPaths` to `super.allKeyPaths`.
return isa<StructDecl>(nominal);
}
// Compute `PartialKeyPathType<Nominal>`, bound to the given nominal
// declaration's type.
static Type computePartialKeyPathType(NominalTypeDecl *nominal) {
auto &C = nominal->getASTContext();
auto nominalType = nominal->getDeclaredInterfaceType();
if (!nominalType || nominalType->hasError())
return nullptr;
auto *partialKeyPathDecl = cast<ClassDecl>(C.getPartialKeyPathDecl());
return BoundGenericClassType::get(partialKeyPathDecl, /*parent*/ Type(),
{nominal->getDeclaredInterfaceType()});
}
// Compute `AllKeyPaths` associated type for the given nominal declaration.
// It should be `[PartialKeyPath<Nominal>]`.
static ArraySliceType *computeAllKeyPathsType(NominalTypeDecl *nominal) {
auto partialKeyPathType = computePartialKeyPathType(nominal);
return ArraySliceType::get(partialKeyPathType);
}
// Mark the given `ValueDecl` as `@inlinable`, if the conformance context's
// module is not resilient and the `ValueDecl` is effectively public.
// TODO: Dedupe with DerivedConformanceRawRepresentable.cpp.
static void maybeMarkAsInlinable(DerivedConformance &derived, ValueDecl *decl) {
ASTContext &C = derived.TC.Context;
auto parentDC = derived.getConformanceContext();
if (!parentDC->getParentModule()->isResilient()) {
auto access = decl->getFormalAccessScope(
nullptr, /*treatUsableFromInlineAsPublic*/ true);
if (access.isPublic()) {
decl->getAttrs().add(new (C) InlinableAttr(/*implicit*/ false));
if (auto *attr = decl->getAttrs().getAttribute<UsableFromInlineAttr>())
attr->setInvalid();
}
}
}
// Synthesize body for the `allKeyPaths` computed property getter.
static std::pair<BraceStmt *, bool>
deriveBodyKeyPathIterable_allKeyPaths(AbstractFunctionDecl *funcDecl, void *) {
auto *parentDC = funcDecl->getDeclContext();
auto *nominal = parentDC->getSelfNominalTypeDecl();
auto &C = nominal->getASTContext();
auto allKeyPathsInterfaceType = computeAllKeyPathsType(nominal);
auto allKeyPathsType = parentDC->mapTypeIntoContext(allKeyPathsInterfaceType);
auto *nominalTypeExpr = TypeExpr::createForDecl(SourceLoc(), nominal,
funcDecl, /*Implicit*/ true);
// Create array of key path expressions to stored properties.
llvm::SmallVector<Expr *, 2> keyPathExprs;
for (auto member : nominal->getStoredProperties()) {
// FIXME(TF-123): Skip generating keypaths to `@differentiable` functions
// because of SILGen crash. Robust fix involves changing
// `createAutoDiffThunk`.
if (auto fnType = member->getType()->getAs<AnyFunctionType>())
if (fnType->getExtInfo().isDifferentiable())
continue;
auto *dotExpr = new (C)
UnresolvedDotExpr(nominalTypeExpr, SourceLoc(), member->getFullName(),
DeclNameLoc(), /*Implicit*/ true);
auto *keyPathExpr =
new (C) KeyPathExpr(SourceLoc(), dotExpr, nullptr, /*Implicit*/ true);
keyPathExprs.push_back(keyPathExpr);
}
// Return array of all key path expressions.
Expr *keyPathsArrayExpr =
ArrayExpr::create(C, SourceLoc(), keyPathExprs, {}, SourceLoc());
// NOTE(TF-575): Adding an explicit coercion expression here is necessary due
// to a missing regression.
keyPathsArrayExpr = new (C) CoerceExpr(
keyPathsArrayExpr, SourceLoc(), TypeLoc::withoutLoc(allKeyPathsType));
auto *returnStmt = new (C) ReturnStmt(SourceLoc(), keyPathsArrayExpr);
auto *body = BraceStmt::create(C, SourceLoc(), {returnStmt}, SourceLoc(),
/*Implicit*/ true);
auto *braceStmt = BraceStmt::create(C, SourceLoc(), {body}, SourceLoc(),
/*Implicit*/ true);
return std::pair<BraceStmt *, bool>(braceStmt, false);
}
// Synthesize the `allKeyPaths` computed property declaration.
static ValueDecl *
deriveKeyPathIterable_allKeyPaths(DerivedConformance &derived) {
auto nominal = derived.Nominal;
auto &C = derived.TC.Context;
auto returnInterfaceTy = computeAllKeyPathsType(nominal);
auto returnTy =
derived.getConformanceContext()->mapTypeIntoContext(returnInterfaceTy);
// Create `allKeyPaths` property declaration.
VarDecl *allKeyPathsDecl;
PatternBindingDecl *pbDecl;
std::tie(allKeyPathsDecl, pbDecl) = derived.declareDerivedProperty(
C.Id_allKeyPaths, returnInterfaceTy, returnTy, /*isStatic*/ false,
/*isFinal*/ true);
// Maybe add `@inlinable` to the `allKeyPaths` declaration.
if (llvm::all_of(nominal->getStoredProperties(), [](VarDecl *vd) {
return vd->getFormalAccessScope(
nullptr, /*treatUsableFromInlineAsPublic*/ true).isPublic();
})) {
maybeMarkAsInlinable(derived, allKeyPathsDecl);
}
// Create `allKeyPaths` getter.
auto *getterDecl = derived.addGetterToReadOnlyDerivedProperty(
allKeyPathsDecl, returnTy);
getterDecl->setBodySynthesizer(
deriveBodyKeyPathIterable_allKeyPaths, nullptr);
derived.addMembersToConformanceContext({getterDecl, allKeyPathsDecl, pbDecl});
return allKeyPathsDecl;
}
static Type deriveKeyPathIterable_AllKeyPaths(DerivedConformance &derived) {
auto *rawInterfaceType = computeAllKeyPathsType(derived.Nominal);
return derived.getConformanceContext()->mapTypeIntoContext(rawInterfaceType);
}
ValueDecl *DerivedConformance::deriveKeyPathIterable(ValueDecl *requirement) {
// Diagnose conformances in disallowed contexts.
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
if (requirement->getBaseName() == TC.Context.Id_allKeyPaths)
return deriveKeyPathIterable_allKeyPaths(*this);
TC.diagnose(requirement->getLoc(),
diag::broken_key_path_iterable_requirement);
return nullptr;
}
Type DerivedConformance::deriveKeyPathIterable(
AssociatedTypeDecl *requirement) {
// Diagnose conformances in disallowed contexts.
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
if (requirement->getBaseName() == TC.Context.Id_AllKeyPaths)
return deriveKeyPathIterable_AllKeyPaths(*this);
TC.diagnose(requirement->getLoc(),
diag::broken_key_path_iterable_requirement);
return nullptr;
}