-
Notifications
You must be signed in to change notification settings - Fork 23
/
polymorphic.ts
323 lines (268 loc) · 9.47 KB
/
polymorphic.ts
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
import { assert, eq, forAll, pp } from "../misc";
import {
ArrayType,
BuiltinFunctionType,
BuiltinStructType,
ErrorType,
EventType,
FunctionType,
MappingType,
PointerType,
TRest,
TupleType,
TVar,
TypeNameType,
TypeNode
} from "./ast";
import { SolTypeError } from "./misc";
import { castable } from "./utils";
export class SolTypePatternMismatchError extends SolTypeError {
constructor(
public readonly expected: TypeNode | TypeNode[],
public readonly actual: TypeNode | TypeNode[]
) {
super(`Pattern mismatch: expected ${pp(expected)} got ${pp(actual)}`);
}
}
/**
* To support type elipses in certain builtin constructs (abi.decode* family and
* type(...)) we introduce a simple notion of polymorphism with 2 types of type
* var - a normal type-var (used for example to type `type(T) => { min: T, max:
* T} where T is numeric) and a type 'elispsis' var, that corresponds to the
* remaining types in a tuple or argument list.
*/
export type TypeSubstituion = Map<string, TypeNode | TypeNode[]>;
/**
* Given two types `a` and `b` where `a` may contain type vars, but
* `b` doesn't have any type vars, compute a substituion from type var names
* to type nodes that converts a to b
*/
export function buildSubstituion(
a: TypeNode,
b: TypeNode,
m: TypeSubstituion,
compilerVersion: string
): void {
if (a instanceof TVar) {
const existingMapping = m.get(a.name);
if (existingMapping !== undefined) {
if (!eq(existingMapping, b)) {
throw new SolTypeError(
`Type var ${a.name} mapped to two different things: ${pp(
existingMapping
)} and ${b.pp()}`
);
}
} else {
m.set(a.name, b);
}
return;
}
if (a instanceof ArrayType && b instanceof ArrayType) {
if (a.size !== b.size) {
throw new SolTypePatternMismatchError(a, b);
}
buildSubstituion(a.elementT, b.elementT, m, compilerVersion);
return;
}
if (a instanceof FunctionType && b instanceof FunctionType) {
if (a.visibility !== b.visibility || a.mutability !== b.mutability) {
throw new SolTypePatternMismatchError(a, b);
}
buildSubstitutions(a.parameters, b.parameters, m, compilerVersion);
buildSubstitutions(a.returns, b.returns, m, compilerVersion);
return;
}
if (a instanceof BuiltinFunctionType && b instanceof BuiltinFunctionType) {
buildSubstitutions(a.parameters, b.parameters, m, compilerVersion);
buildSubstitutions(a.returns, b.returns, m, compilerVersion);
return;
}
if (a instanceof MappingType && b instanceof MappingType) {
buildSubstituion(a.keyType, b.keyType, m, compilerVersion);
buildSubstituion(a.valueType, b.valueType, m, compilerVersion);
return;
}
if (a instanceof PointerType && b instanceof PointerType) {
buildSubstituion(a.to, b.to, m, compilerVersion);
return;
}
if (a instanceof TupleType && b instanceof TupleType) {
assert(
forAll(a.elements, (el) => el !== null) && forAll(b.elements, (el) => el !== null),
`Unexpected tuple with empty elements when building type substitution: {0} or {1}`,
a,
b
);
buildSubstitutions(a.elements as TypeNode[], b.elements as TypeNode[], m, compilerVersion);
return;
}
if (a instanceof TypeNameType && b instanceof TypeNameType) {
buildSubstituion(a.type, b.type, m, compilerVersion);
return;
}
if (a instanceof BuiltinStructType && b instanceof BuiltinStructType) {
if (a.members.size !== b.members.size) {
throw new SolTypePatternMismatchError(a, b);
}
for (const [aName, aVerDepTypes] of a.members) {
const bVerDepTypes = b.members.get(aName);
if (bVerDepTypes === undefined) {
throw new SolTypePatternMismatchError(a, b);
}
if (aVerDepTypes.length !== bVerDepTypes.length) {
throw new SolTypePatternMismatchError(a, b);
}
for (let i = 0; i < aVerDepTypes.length; i++) {
const [aType, aVer] = aVerDepTypes[i];
const [bType, bVer] = bVerDepTypes[i];
if (aVer !== bVer) {
throw new SolTypePatternMismatchError(a, b);
}
buildSubstituion(aType, bType, m, compilerVersion);
}
}
return;
}
if (eq(a, b)) {
return;
}
if (castable(b, a, compilerVersion)) {
return;
}
throw new SolTypePatternMismatchError(a, b);
}
/**
* Given two lists of types as and bs, and a partially computed TypeMap m,
* accumulate the neccessary substitutions to convert as into bs. Note that as
* may contain TVars and TRest, but bs must be concrete.
*/
export function buildSubstitutions(
as: TypeNode[],
bs: TypeNode[],
m: TypeSubstituion,
compilerVersion: string
): void {
for (let i = 0; i < as.length; i++) {
// Note below we allow the last TRest to match to an empty list of types.
if (i >= bs.length && !(i === as.length - 1 && as[i] instanceof TRest)) {
throw new SolTypePatternMismatchError(as, bs);
}
const patternT = as[i];
if (patternT instanceof TRest) {
const existingMapping = m.get(patternT.name);
const actual = bs.slice(i);
if (existingMapping !== undefined) {
if (!eq(existingMapping, actual)) {
throw new SolTypeError(
`Type var ${patternT.name} mapped to two different things: ${pp(
existingMapping
)} and ${pp(actual)}`
);
}
} else {
m.set(patternT.name, actual);
}
return;
}
buildSubstituion(patternT, bs[i], m, compilerVersion);
}
}
/**
* Given a type node `a` that may contain type vars, and a substitution `m`, apply all the
* substitutions in `m` to `a` and return the resulting TypeNode.
*/
export function applySubstitution(a: TypeNode, m: TypeSubstituion): TypeNode {
if (a instanceof TVar) {
const mapped = m.get(a.name);
assert(
!(mapped instanceof Array),
"Unexpected mapping from tvar {0} to list of types {1}",
a.name,
mapped
);
return mapped ? mapped : a;
}
if (a instanceof ArrayType) {
return new ArrayType(applySubstitution(a.elementT, m), a.size);
}
if (a instanceof FunctionType) {
return new FunctionType(
a.name,
applySubstitutions(a.parameters, m),
applySubstitutions(a.returns, m),
a.visibility,
a.mutability,
a.implicitFirstArg
);
}
if (a instanceof BuiltinFunctionType) {
return new BuiltinFunctionType(
a.name,
applySubstitutions(a.parameters, m),
applySubstitutions(a.returns, m)
);
}
if (a instanceof EventType) {
return new EventType(a.name, applySubstitutions(a.parameters, m));
}
if (a instanceof ErrorType) {
return new ErrorType(a.name as string, applySubstitutions(a.parameters, m));
}
if (a instanceof MappingType) {
return new MappingType(applySubstitution(a.keyType, m), applySubstitution(a.valueType, m));
}
if (a instanceof PointerType) {
return new PointerType(applySubstitution(a.to, m), a.location, a.kind);
}
if (a instanceof TupleType) {
assert(
forAll(a.elements, (el) => el !== null),
"Unexpected tuple with empty elements when applying type substitution: {0}",
a
);
return new TupleType(applySubstitutions(a.elements as TypeNode[], m));
}
if (a instanceof TypeNameType) {
return new TypeNameType(applySubstitution(a.type, m));
}
if (a instanceof BuiltinStructType) {
const oldMembers = [...a.members.entries()];
return new BuiltinStructType(
a.name,
new Map(
oldMembers.map(([name, verDepTypes]) => [
name,
verDepTypes.map(([type, ver]) => [applySubstitution(type, m), ver])
])
)
);
}
return a;
}
/**
* Given a list of type nodes `as` that may contain type vars and type elipsis, and a substitution `m`, apply all the
* substitutions in `m` to `as` and return the resulting list of type nodes.
*/
export function applySubstitutions(as: TypeNode[], m: TypeSubstituion): TypeNode[] {
const resTs: TypeNode[] = [];
for (let i = 0; i < as.length; i++) {
const elT = as[i];
if (elT instanceof TRest) {
assert(i === as.length - 1, "Unexpected TRest not in the last place in a type list`");
const mapped = m.get(elT.name);
if (mapped) {
assert(
mapped instanceof Array,
`TRest ${elT.name} not mapped to array. Instead mapped to ${pp(mapped)}`
);
resTs.push(...mapped);
} else {
resTs.push(elT);
}
} else {
resTs.push(applySubstitution(elT, m));
}
}
return resTs;
}