-
Notifications
You must be signed in to change notification settings - Fork 11
/
CSMatch.swift
241 lines (202 loc) · 7.95 KB
/
CSMatch.swift
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
import SwiftcBasic
import SwiftcType
extension ConstraintSystem {
// ref: matchTypes at CSSimplify.cpp
public func matchTypes(kind: Constraint.MatchKind,
left leftType: Type,
right rightType: Type,
options: MatchOptions) -> SolveResult
{
let leftType = simplify(type: leftType)
let rightType = simplify(type: rightType)
func ambiguous() -> SolveResult {
if options.generateConstraintsWhenAmbiguous {
let c = Constraint(kind: kind.asKind(), left: leftType, right: rightType)
_addConstraintEntry(ConstraintEntry(c))
return .solved
}
return .ambiguous
}
let leftVarOrNone = leftType as? TypeVariable
let rightVarOrNone = rightType as? TypeVariable
if leftVarOrNone != nil || rightVarOrNone != nil {
if let leftVar = leftVarOrNone,
let rightVar = rightVarOrNone
{
if leftVar == rightVar {
return .solved
}
}
switch kind {
case .bind:
if let leftVar = leftVarOrNone,
let rightVar = rightVarOrNone
{
mergeEquivalence(type1: leftVar, type2: rightVar)
return .solved
}
let variable: TypeVariable
let fixedType: Type
if let leftVar = leftVarOrNone {
variable = leftVar
fixedType = rightType
} else {
variable = rightVarOrNone!
fixedType = leftType
}
return matchTypesBind(typeVariable: variable,
fixedType: fixedType)
case .conversion:
return ambiguous()
}
}
return matchFixedTypes(kind: kind,
left: leftType,
right: rightType,
options: options)
}
// ref: matchTypes at CSSimplify.cpp
private func matchTypesBind(typeVariable: TypeVariable,
fixedType: Type) -> SolveResult
{
precondition(typeVariable.isRepresentative(bindings: bindings))
if typeVariable.occurs(in: fixedType) {
return .failure
}
assignFixedType(for: typeVariable, fixedType)
return .solved
}
internal func decompositionOptions(_ options: MatchOptions) -> MatchOptions {
var options = options
options.generateConstraintsWhenAmbiguous = true
return options
}
// ref: matchTypes at CSSimplify.cpp
private func matchFixedTypes(kind: Constraint.MatchKind,
left leftType: Type,
right rightType: Type,
options: MatchOptions) -> SolveResult
{
precondition(!(leftType is TypeVariable))
precondition(!(rightType is TypeVariable))
var conversions: [Conversion] = []
if let leftType = leftType as? PrimitiveType,
let rightType = rightType as? PrimitiveType
{
if leftType.name == rightType.name {
conversions.append(.deepEquality)
}
}
if let leftType = leftType as? FunctionType,
let rightType = rightType as? FunctionType
{
return matchFunctionTypes(kind: kind,
left: leftType,
right: rightType,
options: options)
}
if let leftType = leftType as? OptionalType,
let rightType = rightType as? OptionalType
{
conversions.append(.deepEquality)
}
switch kind {
case .conversion:
if leftType is OptionalType,
rightType is OptionalType
{
conversions.append(.optionalToOptional)
}
let leftOptNum = leftType.lookThroughAllOptionals().count
let rightOptNum = rightType.lookThroughAllOptionals().count
if leftOptNum < rightOptNum {
conversions.append(.valueToOptional)
}
case .bind: break
}
func subKind(_ kind: Constraint.MatchKind, conversion: Conversion) -> Constraint.MatchKind {
if conversion == .deepEquality { return .bind }
else { return kind }
}
// 無いなら無理
if conversions.isEmpty {
return .failure
}
// 1つなら即時投入
if conversions.count == 1 {
let conversion = conversions[0]
return simplify(kind: subKind(kind, conversion: conversion),
left: leftType, right: rightType,
conversion: conversion,
options: options)
}
// 2つ以上ならdisjunction
let convCs: [Constraint] = conversions.map { (conv) in
Constraint(kind: subKind(kind, conversion: conv),
left: leftType, right: rightType,
conversion: conv)
}
addDisjunctionConstraint(convCs)
return .solved
}
// ref: matchFunctionTypes at CSSimplify.cpp
private func matchFunctionTypes(kind: Constraint.MatchKind,
left leftType: FunctionType,
right rightType: FunctionType,
options: MatchOptions) -> SolveResult
{
let leftArg = leftType.parameter
let rightArg = rightType.parameter
let leftRet = leftType.result
let rightRet = rightType.result
let subKind: Constraint.MatchKind
switch kind {
case .bind: subKind = .bind
case .conversion: subKind = .conversion
}
let subOptions = decompositionOptions(options)
// param contravariance
switch matchTypes(kind: subKind,
left: rightArg,
right: leftArg,
options: subOptions) {
case .failure: return .failure
case .ambiguous: preconditionFailure()
case .solved: break
}
// result covariance
switch matchTypes(kind: subKind,
left: leftRet,
right: rightRet,
options: subOptions) {
case .failure: return .failure
case .ambiguous: preconditionFailure()
case .solved: break
}
return .solved
}
// ref: matchDeepEqualityTypes at CSSimplify.cpp
internal func matchDeepEqualityTypes(left leftType: Type,
right rightType: Type,
options: MatchOptions) -> SolveResult
{
let subOptions = decompositionOptions(options)
if let leftType = leftType as? PrimitiveType,
let rightType = rightType as? PrimitiveType
{
if leftType.name != rightType.name {
return .failure
}
return .solved
}
if let leftType = leftType as? OptionalType,
let rightType = rightType as? OptionalType
{
return matchTypes(kind: .bind,
left: leftType.wrapped,
right: rightType.wrapped,
options: subOptions)
}
return .failure
}
}