forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ITCDecl.cpp
350 lines (304 loc) · 12.3 KB
/
ITCDecl.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//===--- ITCDecl.cpp - Iterative Type Checker for Declarations ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the portions of the IterativeTypeChecker
// class that involve declarations.
//
//===----------------------------------------------------------------------===//
#include "GenericTypeResolver.h"
#include "TypeChecker.h"
#include "swift/Sema/IterativeTypeChecker.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include <tuple>
using namespace swift;
//===----------------------------------------------------------------------===//
// Inheritance clause handling
//===----------------------------------------------------------------------===//
static std::tuple<TypeResolutionOptions, DeclContext *,
MutableArrayRef<TypeLoc>>
decomposeInheritedClauseDecl(
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl) {
TypeResolutionOptions options;
DeclContext *dc;
MutableArrayRef<TypeLoc> inheritanceClause;
if (auto typeDecl = decl.dyn_cast<TypeDecl *>()) {
inheritanceClause = typeDecl->getInherited();
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
dc = nominal;
options |= TR_GenericSignature | TR_InheritanceClause;
} else {
dc = typeDecl->getDeclContext();
if (isa<GenericTypeParamDecl>(typeDecl)) {
// For generic parameters, we want name lookup to look at just the
// signature of the enclosing entity.
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
dc = nominal;
options |= TR_GenericSignature;
} else if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
dc = ext;
options |= TR_GenericSignature;
} else if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
dc = func;
options |= TR_GenericSignature;
} else if (!dc->isModuleScopeContext()) {
// Skip the generic parameter's context entirely.
dc = dc->getParent();
}
}
}
} else {
auto ext = decl.get<ExtensionDecl *>();
inheritanceClause = ext->getInherited();
dc = ext;
options |= TR_GenericSignature | TR_InheritanceClause;
}
return std::make_tuple(options, dc, inheritanceClause);
}
static std::tuple<TypeResolutionOptions, DeclContext *, TypeLoc *>
decomposeInheritedClauseEntry(
TypeCheckRequest::InheritedClauseEntryPayloadType entry) {
TypeResolutionOptions options;
DeclContext *dc;
MutableArrayRef<TypeLoc> inheritanceClause;
std::tie(options, dc, inheritanceClause)
= decomposeInheritedClauseDecl(entry.first);
return std::make_tuple(options, dc, &inheritanceClause[entry.second]);
}
bool IterativeTypeChecker::isResolveInheritedClauseEntrySatisfied(
TypeCheckRequest::InheritedClauseEntryPayloadType payload) {
TypeLoc &inherited = *std::get<2>(decomposeInheritedClauseEntry(payload));
return !inherited.getType().isNull();
}
void IterativeTypeChecker::processResolveInheritedClauseEntry(
TypeCheckRequest::InheritedClauseEntryPayloadType payload,
UnsatisfiedDependency unsatisfiedDependency) {
TypeResolutionOptions options;
DeclContext *dc;
TypeLoc *inherited;
std::tie(options, dc, inherited) = decomposeInheritedClauseEntry(payload);
// FIXME: Declaration validation is overkill. Sink it down into type
// resolution when it is actually needed.
if (auto nominal = dyn_cast<NominalTypeDecl>(dc))
TC.validateDecl(nominal);
else if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
TC.validateExtension(ext);
}
// Validate the type of this inherited clause entry.
// FIXME: Recursion into existing type checker.
PartialGenericTypeToArchetypeResolver resolver(TC);
if (TC.validateType(*inherited, dc, options, &resolver)) {
inherited->setInvalidType(getASTContext());
}
}
bool IterativeTypeChecker::breakCycleForResolveInheritedClauseEntry(
TypeCheckRequest::InheritedClauseEntryPayloadType payload) {
std::get<2>(decomposeInheritedClauseEntry(payload))
->setInvalidType(getASTContext());
return true;
}
//===----------------------------------------------------------------------===//
// Superclass handling
//===----------------------------------------------------------------------===//
bool IterativeTypeChecker::isTypeCheckSuperclassSatisfied(ClassDecl *payload) {
return payload->LazySemanticInfo.Superclass.getInt();
}
void IterativeTypeChecker::processTypeCheckSuperclass(
ClassDecl *classDecl,
UnsatisfiedDependency unsatisfiedDependency) {
// The superclass should be the first inherited type. However, so
// long as we see already-resolved types that refer to protocols,
// skip over them to keep looking for a misplaced superclass. The
// actual error will be diagnosed when we perform full semantic
// analysis on the class itself.
Type superclassType;
auto inheritedClause = classDecl->getInherited();
for (unsigned i = 0, n = inheritedClause.size(); i != n; ++i) {
TypeLoc &inherited = inheritedClause[i];
// If this inherited type has not been resolved, we depend on it.
if (unsatisfiedDependency(
requestResolveInheritedClauseEntry({ classDecl, i }))) {
return;
}
// If this resolved inherited type is existential, keep going.
if (inherited.getType()->isExistentialType()) continue;
// If this resolved type is a class, we're done.
if (inherited.getType()->getClassOrBoundGenericClass()) {
superclassType = inherited.getType();
break;
}
}
// Set the superclass type.
classDecl->setSuperclass(superclassType);
}
bool IterativeTypeChecker::breakCycleForTypeCheckSuperclass(
ClassDecl *classDecl) {
classDecl->setSuperclass(ErrorType::get(getASTContext()));
return true;
}
//===----------------------------------------------------------------------===//
// Raw type handling
//===----------------------------------------------------------------------===//
bool IterativeTypeChecker::isTypeCheckRawTypeSatisfied(EnumDecl *payload) {
return payload->LazySemanticInfo.RawType.getInt();
}
void IterativeTypeChecker::processTypeCheckRawType(
EnumDecl *enumDecl,
UnsatisfiedDependency unsatisfiedDependency) {
// The raw type should be the first inherited type. However, so
// long as we see already-resolved types that refer to protocols,
// skip over them to keep looking for a misplaced raw type. The
// actual error will be diagnosed when we perform full semantic
// analysis on the enum itself.
Type rawType;
auto inheritedClause = enumDecl->getInherited();
for (unsigned i = 0, n = inheritedClause.size(); i != n; ++i) {
TypeLoc &inherited = inheritedClause[i];
// We depend on having resolved the inherited type.
if (unsatisfiedDependency(
requestResolveInheritedClauseEntry({ enumDecl, i }))) {
return;
}
// If this resolved inherited type is existential, keep going.
if (inherited.getType()->isExistentialType()) continue;
// Record this raw type.
rawType = inherited.getType();
break;
}
// Set the raw type.
enumDecl->setRawType(rawType);
}
bool IterativeTypeChecker::breakCycleForTypeCheckRawType(EnumDecl *enumDecl) {
enumDecl->setRawType(ErrorType::get(getASTContext()));
return true;
}
//===----------------------------------------------------------------------===//
// Inherited protocols
//===----------------------------------------------------------------------===//
bool IterativeTypeChecker::isInheritedProtocolsSatisfied(ProtocolDecl *payload){
return payload->isInheritedProtocolsValid();
}
void IterativeTypeChecker::processInheritedProtocols(
ProtocolDecl *protocol,
UnsatisfiedDependency unsatisfiedDependency) {
// Computing the set of inherited protocols depends on the complete
// inheritance clause.
// FIXME: Technically, we only need very basic name binding.
auto inheritedClause = protocol->getInherited();
bool anyDependencies = false;
llvm::SmallSetVector<ProtocolDecl *, 4> allProtocols;
for (unsigned i = 0, n = inheritedClause.size(); i != n; ++i) {
TypeLoc &inherited = inheritedClause[i];
// We depend on having resolved the inherited type.
if (unsatisfiedDependency(
requestResolveInheritedClauseEntry({ protocol, i }))) {
anyDependencies = true;
continue;
}
// Collect existential types.
// FIXME: We'd prefer to keep what the user wrote here.
SmallVector<ProtocolDecl *, 4> protocols;
if (inherited.getType()->isExistentialType(protocols)) {
allProtocols.insert(protocols.begin(), protocols.end());
continue;
}
}
// If we enumerated any dependencies, we can't complete this request.
if (anyDependencies)
return;
// FIXME: Hack to deal with recursion elsewhere.
if (protocol->isInheritedProtocolsValid())
return;
// Check for circular inheritance.
// FIXME: The diagnostics here should be improved... and this should probably
// be handled by the normal cycle detection.
bool diagnosedCircularity = false;
for (unsigned i = 0, n = allProtocols.size(); i != n; /*in loop*/) {
if (allProtocols[i] == protocol ||
allProtocols[i]->inheritsFrom(protocol)) {
if (!diagnosedCircularity) {
diagnose(protocol,
diag::circular_protocol_def, protocol->getName().str());
diagnosedCircularity = true;
}
allProtocols.remove(allProtocols[i]);
--n;
continue;
}
++i;
}
protocol->setInheritedProtocols(getASTContext().AllocateCopy(allProtocols));
}
bool IterativeTypeChecker::breakCycleForInheritedProtocols(
ProtocolDecl *protocol) {
// FIXME: We'd like to drop just the problematic protocols, not
// everything.
protocol->setInheritedProtocols({});
return true;
}
//===----------------------------------------------------------------------===//
// Resolve a type declaration
//===----------------------------------------------------------------------===//
bool IterativeTypeChecker::isResolveTypeDeclSatisfied(TypeDecl *typeDecl) {
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
// If the underlying type was validated, we're done.
return typeAliasDecl->getUnderlyingTypeLoc().wasValidated();
}
if (auto typeParam = dyn_cast<AbstractTypeParamDecl>(typeDecl)) {
// FIXME: Deal with these.
return typeParam->getArchetype();
}
// Module types are always fully resolved.
if (isa<ModuleDecl>(typeDecl))
return true;
// Nominal types.
auto nominal = cast<NominalTypeDecl>(typeDecl);
return !nominal->getDeclaredType().isNull();
}
void IterativeTypeChecker::processResolveTypeDecl(
TypeDecl *typeDecl,
UnsatisfiedDependency unsatisfiedDependency) {
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
if (typeAliasDecl->getDeclContext()->isModuleScopeContext()) {
// FIXME: This is silly.
if (!typeAliasDecl->hasType())
typeAliasDecl->computeType();
TypeResolutionOptions options;
options |= TR_GlobalTypeAlias;
if (typeAliasDecl->getFormalAccess() == Accessibility::Private)
options |= TR_KnownNonCascadingDependency;
// Note: recursion into old type checker is okay when passing in an
// unsatisfied-dependency callback.
if (TC.validateType(typeAliasDecl->getUnderlyingTypeLoc(),
typeAliasDecl->getDeclContext(),
options, nullptr, &unsatisfiedDependency)) {
typeAliasDecl->setInvalid();
typeAliasDecl->overwriteType(ErrorType::get(getASTContext()));
typeAliasDecl->getUnderlyingTypeLoc().setInvalidType(getASTContext());
}
return;
}
// Fall through.
}
// FIXME: Recursion into the old type checker.
TC.validateDecl(typeDecl);
}
bool IterativeTypeChecker::breakCycleForResolveTypeDecl(TypeDecl *typeDecl) {
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
typeAliasDecl->setInvalid();
typeAliasDecl->overwriteType(ErrorType::get(getASTContext()));
typeAliasDecl->getUnderlyingTypeLoc().setInvalidType(getASTContext());
return true;
}
// FIXME: Generalize this.
return false;
}