forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterativeTypeChecker.cpp
143 lines (123 loc) · 4.71 KB
/
IterativeTypeChecker.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
//===--- IterativeTypeChecker.cpp - Iterative Type Checker ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the IterativeTypeChecker class, which
// performs iterative type checking by tracking the set of
// outstanding type-checking requests and servicing them as needed.
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "swift/Sema/IterativeTypeChecker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/Basic/Defer.h"
using namespace swift;
ASTContext &IterativeTypeChecker::getASTContext() const {
return TC.Context;
}
DiagnosticEngine &IterativeTypeChecker::getDiags() const {
return getASTContext().Diags;
}
void IterativeTypeChecker::process(
TypeCheckRequest request,
UnsatisfiedDependency unsatisfiedDependency) {
switch (request.getKind()) {
#define TYPE_CHECK_REQUEST(Request,PayloadName) \
case TypeCheckRequest::Request: \
return process##Request(request.get##PayloadName##Payload(), \
unsatisfiedDependency);
#include "swift/Sema/TypeCheckRequestKinds.def"
}
}
/// Determine whether the given request has already been satisfied.
bool IterativeTypeChecker::isSatisfied(TypeCheckRequest request) {
switch (request.getKind()) {
#define TYPE_CHECK_REQUEST(Request,PayloadName) \
case TypeCheckRequest::Request: \
return is##Request##Satisfied(request.get##PayloadName##Payload());
#include "swift/Sema/TypeCheckRequestKinds.def"
}
}
bool IterativeTypeChecker::breakCycle(TypeCheckRequest request) {
switch (request.getKind()) {
#define TYPE_CHECK_REQUEST(Request,PayloadName) \
case TypeCheckRequest::Request: \
return breakCycleFor##Request(request.get##PayloadName##Payload());
#include "swift/Sema/TypeCheckRequestKinds.def"
}
}
void IterativeTypeChecker::satisfy(TypeCheckRequest request) {
// If the request has already been satisfied, we're done.
if (isSatisfied(request)) return;
// Check for circular dependencies in our requests.
// FIXME: This stack operation is painfully inefficient.
auto existingRequest = std::find(ActiveRequests.rbegin(),
ActiveRequests.rend(),
request);
if (existingRequest != ActiveRequests.rend()) {
auto first = existingRequest.base();
--first;
diagnoseCircularReference(llvm::makeArrayRef(&*first,
&*ActiveRequests.end()));
return;
}
// Add this request to the stack of active requests.
ActiveRequests.push_back(request);
defer([&] { ActiveRequests.pop_back(); });
while (true) {
// Process this requirement, enumerating dependencies if anything else needs
// to be handled first.
SmallVector<TypeCheckRequest, 4> unsatisfied;
process(request, [&](TypeCheckRequest dependency) -> bool {
if (isSatisfied(dependency)) return false;
// Record the unsatisfied dependency.
unsatisfied.push_back(dependency);
return true;
});
// If there were no unsatisfied dependencies, we're done.
if (unsatisfied.empty()) {
assert(isSatisfied(request));
break;
}
// Recurse to satisfy any unsatisfied dependencies.
// FIXME: Don't recurse in the iterative type checker, silly!
for (auto dependency : unsatisfied) {
satisfy(dependency);
}
}
}
//----------------------------------------------------------------------------//
// Diagnostics
//----------------------------------------------------------------------------//
void IterativeTypeChecker::diagnoseCircularReference(
ArrayRef<TypeCheckRequest> requests) {
bool isFirst = true;
for (const auto &request : requests) {
diagnose(request.getLoc(),
isFirst ? diag::circular_reference
: diag::circular_reference_through);
isFirst = false;
}
// Now try to break the cycle.
#ifndef NDEBUG
bool brokeCycle = false;
#endif
for (const auto &request : reverse(requests)) {
if (breakCycle(request)) {
#ifndef NDEBUG
brokeCycle = true;
#endif
break;
}
}
assert(brokeCycle && "Will the cycle be unbroken?");
}