forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DerivedConformances.cpp
234 lines (194 loc) · 8.82 KB
/
DerivedConformances.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//===--- DerivedConformances.cpp - Derived conformance utilities ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Types.h"
#include "DerivedConformances.h"
using namespace swift;
using namespace DerivedConformance;
ValueDecl *DerivedConformance::getDerivableRequirement(NominalTypeDecl *nominal,
ValueDecl *requirement) {
ASTContext &ctx = nominal->getASTContext();
auto name = requirement->getFullName();
// Local function that retrieves the requirement with the same name as
// the provided requirement, but within the given known protocol.
auto getRequirement = [&](KnownProtocolKind kind) -> ValueDecl * {
// Dig out the protocol.
auto proto = ctx.getProtocol(kind);
if (!proto) return nullptr;
// Check whether this nominal type derives conformances to the
if (!nominal->derivesProtocolConformance(proto)) return nullptr;
// Retrieve the requirement.
for (auto result : proto->lookupDirect(name))
return result;
return nullptr;
};
// Properties.
if (isa<VarDecl>(requirement)) {
// RawRepresentable.rawValue
if (name.isSimpleName(ctx.Id_rawValue))
return getRequirement(KnownProtocolKind::RawRepresentable);
// Hashable.hashValue
if (name.isSimpleName(ctx.Id_hashValue))
return getRequirement(KnownProtocolKind::Hashable);
// ErrorType._code
if (name.isSimpleName(ctx.Id_code_))
return getRequirement(KnownProtocolKind::ErrorType);
// _BridgedNSError._NSErrorDomain
if (name.isSimpleName(ctx.Id_NSErrorDomain))
return getRequirement(KnownProtocolKind::_BridgedNSError);
return nullptr;
}
// Functions.
if (auto func = dyn_cast<FuncDecl>(requirement)) {
if (func->isOperator() && name.getBaseName().str() == "==")
return getRequirement(KnownProtocolKind::Equatable);
return nullptr;
}
// Initializers.
if (isa<ConstructorDecl>(requirement)) {
auto argumentNames = name.getArgumentNames();
if (argumentNames.size() == 1 && argumentNames[0] == ctx.Id_rawValue)
return getRequirement(KnownProtocolKind::RawRepresentable);
return nullptr;
}
// Associated types.
if (isa<AssociatedTypeDecl>(requirement)) {
// RawRepresentable.RawValue
if (name.isSimpleName(ctx.Id_RawValue))
return getRequirement(KnownProtocolKind::RawRepresentable);
return nullptr;
}
return nullptr;
}
void DerivedConformance::_insertOperatorDecl(ASTContext &C,
IterableDeclContext *scope,
Decl *member) {
// Find the module.
auto mod = member->getModuleContext();
// Add it to the module in a DerivedFileUnit.
mod->getDerivedFileUnit().addDerivedDecl(cast<FuncDecl>(member));
// Add it as a derived global decl to the nominal type.
auto oldDerived = scope->getDerivedGlobalDecls();
auto oldSize = std::distance(oldDerived.begin(), oldDerived.end());
auto newDerived = C.Allocate<Decl*>(oldSize + 1);
std::move(oldDerived.begin(), oldDerived.end(), newDerived.begin());
newDerived[oldSize] = member;
scope->setDerivedGlobalDecls(newDerived);
}
DeclRefExpr *
DerivedConformance::createSelfDeclRef(AbstractFunctionDecl *fn) {
ASTContext &C = fn->getASTContext();
Pattern *curriedArgs = fn->getBodyParamPatterns().front();
auto selfPattern =
cast<NamedPattern>(curriedArgs->getSemanticsProvidingPattern());
auto selfDecl = selfPattern->getDecl();
return new (C) DeclRefExpr(selfDecl, SourceLoc(), /*implicit*/true);
}
FuncDecl *DerivedConformance::declareDerivedPropertyGetter(TypeChecker &tc,
Decl *parentDecl,
NominalTypeDecl *typeDecl,
Type contextType,
Type propertyInterfaceType,
Type propertyContextType,
bool isStatic) {
auto &C = tc.Context;
Type selfType = contextType;
if (isStatic)
selfType = MetatypeType::get(selfType);
auto parentDC = cast<DeclContext>(parentDecl);
VarDecl *selfDecl = new (C) ParamDecl(/*IsLet*/true,
SourceLoc(),
Identifier(),
SourceLoc(),
C.Id_self,
selfType,
parentDC);
selfDecl->setImplicit();
Pattern *selfParam = new (C) NamedPattern(selfDecl, /*implicit*/ true);
selfParam->setType(selfType);
selfParam = new (C) TypedPattern(selfParam,
TypeLoc::withoutLoc(selfType));
selfParam->setType(selfType);
Pattern *methodParam = TuplePattern::create(C, SourceLoc(),{},SourceLoc());
methodParam->setType(TupleType::getEmpty(C));
Pattern *params[] = {selfParam, methodParam};
FuncDecl *getterDecl =
FuncDecl::create(C, SourceLoc(), StaticSpellingKind::None, SourceLoc(),
DeclName(), SourceLoc(), SourceLoc(), nullptr, Type(),
params, TypeLoc::withoutLoc(propertyContextType),
parentDC);
getterDecl->setImplicit();
getterDecl->setStatic(isStatic);
// Compute the type of the getter.
GenericParamList *genericParams = nullptr;
Type type = FunctionType::get(TupleType::getEmpty(C),
propertyContextType);
selfType = getterDecl->computeSelfType(&genericParams);
if (genericParams)
type = PolymorphicFunctionType::get(selfType, type, genericParams);
else
type = FunctionType::get(selfType, type);
getterDecl->setType(type);
getterDecl->setBodyResultType(propertyContextType);
// Compute the interface type of the getter.
Type interfaceType = FunctionType::get(TupleType::getEmpty(C),
propertyInterfaceType);
Type selfInterfaceType = getterDecl->computeInterfaceSelfType(false);
if (auto sig = parentDC->getGenericSignatureOfContext())
interfaceType = GenericFunctionType::get(sig, selfInterfaceType,
interfaceType,
FunctionType::ExtInfo());
else
interfaceType = type;
getterDecl->setInterfaceType(interfaceType);
getterDecl->setAccessibility(typeDecl->getFormalAccess());
if (typeDecl->hasClangNode())
tc.implicitlyDefinedFunctions.push_back(getterDecl);
return getterDecl;
}
std::pair<VarDecl *, PatternBindingDecl *>
DerivedConformance::declareDerivedReadOnlyProperty(TypeChecker &tc,
Decl *parentDecl,
NominalTypeDecl *typeDecl,
Identifier name,
Type propertyInterfaceType,
Type propertyContextType,
FuncDecl *getterDecl,
bool isStatic) {
auto &C = tc.Context;
auto parentDC = cast<DeclContext>(parentDecl);
VarDecl *propDecl = new (C) VarDecl(isStatic,
/*let*/ false,
SourceLoc(), name,
propertyContextType,
parentDC);
propDecl->setImplicit();
propDecl->makeComputed(SourceLoc(), getterDecl, nullptr, nullptr,
SourceLoc());
propDecl->setAccessibility(typeDecl->getFormalAccess());
propDecl->setInterfaceType(propertyInterfaceType);
Pattern *propPat = new (C) NamedPattern(propDecl, /*implicit*/ true);
propPat->setType(propertyContextType);
propPat = new (C) TypedPattern(propPat,
TypeLoc::withoutLoc(propertyContextType),
/*implicit*/ true);
auto pbDecl = PatternBindingDecl::create(C, SourceLoc(),
StaticSpellingKind::None,
SourceLoc(), propPat, nullptr,
parentDC);
pbDecl->setImplicit();
return {propDecl, pbDecl};
}