-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.ts
608 lines (543 loc) · 18.5 KB
/
types.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
* Mappings from AssemblyScript types to WebAssembly types.
* @module types
*//***/
import {
Class,
FunctionTarget
} from "./program";
import {
NativeType,
ExpressionRef,
Module
} from "./module";
/** Indicates the kind of a type. */
export const enum TypeKind {
// signed integers
/** An 8-bit signed integer. */
I8,
/** A 16-bit signed integer. */
I16,
/** A 32-bit signed integer. */
I32,
/** A 64-bit signed integer. */
I64,
/** A 32-bit/64-bit signed integer, depending on the target. */
ISIZE,
// unsigned integers
/** An 8-bit unsigned integer. */
U8,
/** A 16-bit unsigned integer. */
U16,
/** A 32-bit unsigned integer. Also the base of function types. */
U32,
/** A 64-bit unsigned integer. */
U64,
/** A 32-bit/64-bit unsigned integer, depending on the target. Also the base of class types. */
USIZE,
/** A 1-bit unsigned integer. */
BOOL, // sic
// floats
/** A 32-bit float. */
F32,
/** A 64-bit double. */
F64,
// other
/** No return type. */
VOID
}
/** Indicates capabilities of a type. */
export const enum TypeFlags {
NONE = 0,
/** Is a signed type that can represent negative values. */
SIGNED = 1 << 0,
/** Is an unsigned type that cannot represent negative values. */
UNSIGNED = 1 << 1,
/** Is an integer type. */
INTEGER = 1 << 2,
/** Is a floating point type. */
FLOAT = 1 << 3,
/** Is a pointer type. */
POINTER = 1 << 4,
/** Is smaller than 32-bits. */
SHORT = 1 << 5,
/** Is larger than 32-bits. */
LONG = 1 << 6,
/** Is a value type. */
VALUE = 1 << 7,
/** Is a reference type. */
REFERENCE = 1 << 8,
/** Is a nullable type. */
NULLABLE = 1 << 9
}
/** Represents a resolved type. */
export class Type {
/** Type kind. */
kind: TypeKind;
/** Type flags. */
flags: TypeFlags;
/** Size in bits. */
size: u32;
/** Size in bytes. */
byteSize: i32;
/** Underlying class reference, if a class type. */
classReference: Class | null;
/** Underlying signature reference, if a function type. */
signatureReference: Signature | null;
/** Respective non-nullable type, if nullable. */
nonNullableType: Type;
/** Cached nullable type, if non-nullable. */
private cachedNullableType: Type | null = null;
/** Constructs a new resolved type. */
constructor(kind: TypeKind, flags: TypeFlags, size: u32) {
this.kind = kind;
this.flags = flags;
this.size = size;
this.byteSize = <i32>ceil<f64>(<f64>size / 8);
this.classReference = null;
this.signatureReference = null;
this.nonNullableType = this;
}
/** Returns the int type of this type. Defaults to `Type.i32` if this is not an int type. */
get intType(): Type {
switch (this.kind) {
case TypeKind.I8:
case TypeKind.I16:
case TypeKind.I32:
case TypeKind.I64:
case TypeKind.ISIZE:
case TypeKind.U8:
case TypeKind.U16:
case TypeKind.U32:
case TypeKind.U64:
case TypeKind.USIZE: return this;
case TypeKind.BOOL:
default: return Type.i32;
}
}
/** Computes the sign-extending shift in the target type. */
computeSmallIntegerShift(targetType: Type): u32 {
return targetType.size - this.size;
}
/** Computes the truncating mask in the target type. */
computeSmallIntegerMask(targetType: Type): u32 {
var size = this.is(TypeFlags.UNSIGNED) ? this.size : this.size - 1;
return ~0 >>> (targetType.size - size);
}
/** Tests if this type has (all of) the specified flags. */
is(flags: TypeFlags): bool { return (this.flags & flags) == flags; }
/** Tests if this type has any of the specified flags. */
isAny(flags: TypeFlags): bool { return (this.flags & flags) != 0; }
/** Composes a class type from this type and a class. */
asClass(classType: Class): Type {
assert(this.kind == TypeKind.USIZE && !this.classReference);
var ret = new Type(this.kind, this.flags & ~TypeFlags.VALUE | TypeFlags.REFERENCE, this.size);
ret.classReference = classType;
return ret;
}
/** Composes a function type from this type and a function. */
asFunction(signature: Signature): Type {
assert(this.kind == TypeKind.U32 && !this.signatureReference);
var ret = new Type(this.kind, this.flags & ~TypeFlags.VALUE | TypeFlags.REFERENCE, this.size);
ret.signatureReference = signature;
return ret;
}
/** Composes the respective nullable type of this type. */
asNullable(): Type {
assert(this.is(TypeFlags.REFERENCE));
if (!this.cachedNullableType) {
assert(!this.is(TypeFlags.NULLABLE));
this.cachedNullableType = new Type(this.kind, this.flags | TypeFlags.NULLABLE, this.size);
this.cachedNullableType.classReference = this.classReference; // either a class reference
this.cachedNullableType.signatureReference = this.signatureReference; // or a function reference
}
return this.cachedNullableType;
}
/** Tests if a value of this type is assignable to a target of the specified type. */
isAssignableTo(target: Type, signednessIsImportant: bool = false): bool {
var currentClass: Class | null;
var targetClass: Class | null;
var currentFunction: Signature | null;
var targetFunction: Signature | null;
if (this.is(TypeFlags.REFERENCE)) {
if (target.is(TypeFlags.REFERENCE)) {
if (currentClass = this.classReference) {
if (targetClass = target.classReference) {
return currentClass.isAssignableTo(targetClass);
}
} else if (currentFunction = this.signatureReference) {
if (targetFunction = target.signatureReference) {
return currentFunction.isAssignableTo(targetFunction);
}
}
}
} else if (!target.is(TypeFlags.REFERENCE)) {
if (this.is(TypeFlags.INTEGER)) {
if (target.is(TypeFlags.INTEGER)) {
if (!signednessIsImportant || this.is(TypeFlags.SIGNED) == target.is(TypeFlags.SIGNED)) {
return this.size <= target.size;
}
} else if (target.kind == TypeKind.F32) {
return this.size <= 23; // mantissa bits
} else if (target.kind == TypeKind.F64) {
return this.size <= 52; // ^
}
} else if (this.is(TypeFlags.FLOAT)) {
if (target.is(TypeFlags.FLOAT)) {
return this.size <= target.size;
}
}
}
return false;
}
/** Determines the common compatible type of two types, if any. */
static commonCompatible(left: Type, right: Type, signednessIsImportant: bool): Type | null {
if (right.isAssignableTo(left, signednessIsImportant)) return left;
else if (left.isAssignableTo(right, signednessIsImportant)) return right;
return null;
}
/** Converts this type to its TypeScript representation. */
toString(kindOnly: bool = false): string {
switch (this.kind) {
case TypeKind.I8: return "i8";
case TypeKind.I16: return "i16";
case TypeKind.I32: return "i32";
case TypeKind.I64: return "i64";
case TypeKind.ISIZE: return "isize";
case TypeKind.U8: return "u8";
case TypeKind.U16: return "u16";
case TypeKind.U32: {
let functionType = this.signatureReference;
return kindOnly || !functionType ? "u32" : functionType.toString(true);
}
case TypeKind.U64: return "u64";
case TypeKind.USIZE: {
let classType = this.classReference;
return kindOnly || !classType ? "usize" : classType.toString();
}
case TypeKind.BOOL: return "bool";
case TypeKind.F32: return "f32";
case TypeKind.F64: return "f64";
case TypeKind.VOID: return "void";
default: {
assert(false);
return "";
}
}
}
// Binaryen specific
/** Converts this type to its respective native type. */
toNativeType(): NativeType {
switch (this.kind) {
default: return NativeType.I32;
case TypeKind.I64:
case TypeKind.U64: return NativeType.I64;
case TypeKind.ISIZE:
case TypeKind.USIZE: return this.size == 64 ? NativeType.I64 : NativeType.I32;
case TypeKind.F32: return NativeType.F32;
case TypeKind.F64: return NativeType.F64;
case TypeKind.VOID: return NativeType.None;
}
}
/** Converts this type to its native `0` value. */
toNativeZero(module: Module): ExpressionRef {
switch (this.kind) {
case TypeKind.VOID: assert(false);
default: return module.createI32(0);
case TypeKind.ISIZE:
case TypeKind.USIZE: if (this.size != 64) return module.createI32(0);
case TypeKind.I64:
case TypeKind.U64: return module.createI64(0);
case TypeKind.F32: return module.createF32(0);
case TypeKind.F64: return module.createF64(0);
}
}
/** Converts this type to its native `1` value. */
toNativeOne(module: Module): ExpressionRef {
switch (this.kind) {
case TypeKind.VOID: assert(false);
default: return module.createI32(1);
case TypeKind.ISIZE:
case TypeKind.USIZE: if (this.size != 64) return module.createI32(1);
case TypeKind.I64:
case TypeKind.U64: return module.createI64(1);
case TypeKind.F32: return module.createF32(1);
case TypeKind.F64: return module.createF64(1);
}
}
/** Converts this type to its native `-1` value. */
toNativeNegOne(module: Module): ExpressionRef {
switch (this.kind) {
case TypeKind.VOID: assert(false);
default: return module.createI32(-1);
case TypeKind.ISIZE:
case TypeKind.USIZE: if (this.size != 64) return module.createI32(-1);
case TypeKind.I64:
case TypeKind.U64: return module.createI64(-1, -1);
case TypeKind.F32: return module.createF32(-1);
case TypeKind.F64: return module.createF64(-1);
}
}
/** Converts this type to its signature string. */
toSignatureString(): string {
switch (this.kind) {
default: return "i";
case TypeKind.I64:
case TypeKind.U64: return "I";
case TypeKind.ISIZE:
case TypeKind.USIZE: return this.size == 64 ? "I" : "i";
case TypeKind.F32: return "f";
case TypeKind.F64: return "F";
case TypeKind.VOID: return "v";
}
}
// Types
/** An 8-bit signed integer. */
static readonly i8: Type = new Type(TypeKind.I8,
TypeFlags.SIGNED |
TypeFlags.SHORT |
TypeFlags.INTEGER |
TypeFlags.VALUE, 8
);
/** A 16-bit signed integer. */
static readonly i16: Type = new Type(TypeKind.I16,
TypeFlags.SIGNED |
TypeFlags.SHORT |
TypeFlags.INTEGER |
TypeFlags.VALUE, 16
);
/** A 32-bit signed integer. */
static readonly i32: Type = new Type(TypeKind.I32,
TypeFlags.SIGNED |
TypeFlags.INTEGER |
TypeFlags.VALUE, 32
);
/** A 64-bit signed integer. */
static readonly i64: Type = new Type(TypeKind.I64,
TypeFlags.SIGNED |
TypeFlags.LONG |
TypeFlags.INTEGER |
TypeFlags.VALUE, 64
);
/** A 32-bit signed size. WASM32 only. */
static readonly isize32: Type = new Type(TypeKind.ISIZE,
TypeFlags.SIGNED |
TypeFlags.INTEGER |
TypeFlags.POINTER |
TypeFlags.VALUE, 32
);
/** A 64-bit signed size. WASM64 only. */
static readonly isize64: Type = new Type(TypeKind.ISIZE,
TypeFlags.SIGNED |
TypeFlags.LONG |
TypeFlags.INTEGER |
TypeFlags.POINTER |
TypeFlags.VALUE, 64
);
/** An 8-bit unsigned integer. */
static readonly u8: Type = new Type(TypeKind.U8,
TypeFlags.UNSIGNED |
TypeFlags.SHORT |
TypeFlags.INTEGER |
TypeFlags.VALUE, 8
);
/** A 16-bit unsigned integer. */
static readonly u16: Type = new Type(TypeKind.U16,
TypeFlags.UNSIGNED |
TypeFlags.SHORT |
TypeFlags.INTEGER |
TypeFlags.VALUE, 16
);
/** A 32-bit unsigned integer. */
static readonly u32: Type = new Type(TypeKind.U32,
TypeFlags.UNSIGNED |
TypeFlags.INTEGER |
TypeFlags.VALUE, 32
);
/** A 64-bit unsigned integer. */
static readonly u64: Type = new Type(TypeKind.U64,
TypeFlags.UNSIGNED |
TypeFlags.LONG |
TypeFlags.INTEGER |
TypeFlags.VALUE, 64
);
/** A 32-bit unsigned size. WASM32 only. */
static readonly usize32: Type = new Type(TypeKind.USIZE,
TypeFlags.UNSIGNED |
TypeFlags.INTEGER |
TypeFlags.POINTER |
TypeFlags.VALUE, 32
);
/** A 64-bit unsigned size. WASM64 only. */
static readonly usize64: Type = new Type(TypeKind.USIZE,
TypeFlags.UNSIGNED |
TypeFlags.LONG |
TypeFlags.INTEGER |
TypeFlags.POINTER |
TypeFlags.VALUE, 64
);
/** A 1-bit unsigned integer. */
static readonly bool: Type = new Type(TypeKind.BOOL,
TypeFlags.UNSIGNED |
TypeFlags.SHORT |
TypeFlags.INTEGER |
TypeFlags.VALUE, 1
);
/** A 32-bit float. */
static readonly f32: Type = new Type(TypeKind.F32,
TypeFlags.SIGNED |
TypeFlags.FLOAT |
TypeFlags.VALUE, 32
);
/** A 64-bit float. */
static readonly f64: Type = new Type(TypeKind.F64,
TypeFlags.SIGNED |
TypeFlags.LONG |
TypeFlags.FLOAT |
TypeFlags.VALUE, 64
);
/** No return type. */
static readonly void: Type = new Type(TypeKind.VOID, TypeFlags.NONE, 0);
}
/** Converts an array of types to an array of native types. */
export function typesToNativeTypes(types: Type[]): NativeType[] {
var numTypes = types.length;
var ret = new Array<NativeType>(numTypes);
for (let i = 0; i < numTypes; ++i) ret[i] = types[i].toNativeType();
return ret;
}
/** Converts an array of types to its combined string representation. */
export function typesToString(types: Type[]): string {
var numTypes = types.length;
if (!numTypes) return "";
var sb = new Array<string>(numTypes);
for (let i = 0; i < numTypes; ++i) sb[i] = types[i].toString();
return sb.join(",");
}
/** Represents a fully resolved function signature. */
export class Signature {
/** Parameter types, if any, excluding `this`. */
parameterTypes: Type[];
/** Parameter names, if known, excluding `this`. */
parameterNames: string[] | null;
/** Number of required parameters excluding `this`. Other parameters are considered optional. */
requiredParameters: i32;
/** Return type. */
returnType: Type;
/** This type, if an instance signature. */
thisType: Type | null;
/** Whether the last parameter is a rest parameter. */
hasRest: bool;
/** Cached {@link FunctionTarget}. */
cachedFunctionTarget: FunctionTarget | null = null;
/** Respective function type. */
type: Type;
/** Constructs a new signature. */
constructor(
parameterTypes: Type[] | null = null,
returnType: Type | null = null,
thisType: Type | null = null
) {
this.parameterTypes = parameterTypes ? parameterTypes : [];
this.parameterNames = null;
this.requiredParameters = 0;
this.returnType = returnType ? returnType : Type.void;
this.thisType = thisType;
this.hasRest = false;
this.type = Type.u32.asFunction(this);
}
/** Gets the known or, alternatively, generic parameter name at the specified index. */
getParameterName(index: i32): string {
var parameterNames = this.parameterNames;
return parameterNames && parameterNames.length > index
? parameterNames[index]
: getGenericParameterName(index);
}
/** Tests if a value of this function type is assignable to a target of the specified function type. */
isAssignableTo(target: Signature): bool {
// TODO: maybe cache results?
// check `this` type
var thisThisType = this.thisType;
var targetThisType = target.thisType;
if (thisThisType) {
if (!(targetThisType && thisThisType.isAssignableTo(targetThisType))) return false;
} else if (targetThisType) {
return false;
}
// check rest parameter
if (this.hasRest != target.hasRest) return false; // TODO
// check parameter types
var thisParameterTypes = this.parameterTypes;
var targetParameterTypes = target.parameterTypes;
var numParameters = thisParameterTypes.length;
if (numParameters != targetParameterTypes.length) return false;
for (let i = 0; i < numParameters; ++i) {
let thisParameterType = thisParameterTypes[i];
let targetParameterType = targetParameterTypes[i];
if (!thisParameterType.isAssignableTo(targetParameterType)) return false;
}
// check return type
var thisReturnType = this.returnType;
var targetReturnType = target.returnType;
return thisReturnType == targetReturnType || thisReturnType.isAssignableTo(targetReturnType);
}
/** Converts a signature to a function type string. */
static makeSignatureString(parameterTypes: Type[] | null, returnType: Type, thisType: Type | null = null): string {
var sb = [];
if (thisType) sb.push(thisType.toSignatureString());
if (parameterTypes) {
for (let i = 0, k = parameterTypes.length; i < k; ++i) sb.push(parameterTypes[i].toSignatureString());
}
sb.push(returnType.toSignatureString());
return sb.join("");
}
/** Converts this signature to a function type string. */
toSignatureString(): string {
return Signature.makeSignatureString(this.parameterTypes, this.returnType, this.thisType);
}
/** Converts this signature to a string. */
toString(includeThis: bool = false): string {
var sb = new Array<string>();
sb.push("(");
var index = 0;
var thisType = this.thisType;
if (thisType) {
if (includeThis) {
sb.push("this: ");
sb.push(thisType.toString());
index = 1;
}
}
var parameters = this.parameterTypes;
var numParameters = parameters.length;
if (numParameters) {
let names = this.parameterNames;
let numNames = names ? names.length : 0;
let optionalStart = this.requiredParameters;
let restIndex = this.hasRest ? numParameters - 1 : -1;
for (let i = 0; i < numParameters; ++i, ++index) {
if (index) sb.push(", ");
if (i == restIndex) sb.push("...");
if (i < numNames) sb.push((<string[]>names)[i]);
else sb.push(getGenericParameterName(i));
if (i >= optionalStart && i != restIndex) sb.push("?: ");
else sb.push(": ");
sb.push(parameters[i].toString());
}
}
sb.push(") => ");
sb.push(this.returnType.toString());
return sb.join("");
}
}
// helpers
// Cached generic parameter names used where names are unknown.
var cachedGenericParameterNames: string[] | null = null;
/** Gets the cached generic parameter name for the specified index. */
export function getGenericParameterName(index: i32): string {
if (!cachedGenericParameterNames) cachedGenericParameterNames = [];
for (let i = cachedGenericParameterNames.length; i <= index; ++i) {
cachedGenericParameterNames.push("arg$" + i.toString(10));
}
return cachedGenericParameterNames[index - 1];
}