-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathReferenceType.ts
More file actions
666 lines (602 loc) · 29.2 KB
/
ReferenceType.ts
File metadata and controls
666 lines (602 loc) · 29.2 KB
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import type { GetTypeOptions, TypeChainEntry, TypeCompatibilityData } from '../interfaces';
import type { GetSymbolTypeOptions, SymbolTable, SymbolTableProvider, SymbolTypeGetterProvider } from '../SymbolTable';
import type { SymbolTypeFlag } from '../SymbolTypeFlag';
import { isAnyReferenceType, isArrayDefaultTypeReferenceType, isArrayType, isBinaryOperatorReferenceType, isComponentType, isDynamicType, isParamTypeFromValueReferenceType, isReferenceType, isTypePropertyReferenceType } from '../astUtils/reflection';
import { BscType } from './BscType';
import { DynamicType } from './DynamicType';
import { BscTypeKind } from './BscTypeKind';
import type { Token } from '../lexer/Token';
import { util } from '../util';
export type AnyReferenceType = ReferenceType | TypePropertyReferenceType | BinaryOperatorReferenceType | ArrayDefaultTypeReferenceType;
export function referenceTypeFactory(memberKey: string, fullName, flags: SymbolTypeFlag, tableProvider: SymbolTypeGetterProvider) {
return new ReferenceType(memberKey, fullName, flags, tableProvider);
}
export class ReferenceType extends BscType {
/**
* ReferenceTypes are used when the actual type may be resolved later from a Symbol table
* @param memberKey which key do we use to look up this type in the given table?
* @param fullName the full/display name for this type
* @param flags is this type available at typetime, runtime, etc.
* @param tableProvider function that returns a SymbolTable that we use for the lookup.
*/
constructor(public memberKey: string, public fullName, public flags: SymbolTypeFlag, public tableProvider: SymbolTypeGetterProvider) {
super(memberKey);
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get: (target, propName, receiver) => {
if (propName === '__reflection') {
// Cheeky way to get `isReferenceType` reflection to work
return this.__reflection;
}
if (propName === '__identifier') {
// Cheeky way to get `isReferenceType` reflection to work
return this.__identifier;
}
if (propName === 'fullName') {
return this.fullName;
}
if (propName === 'isResolvable') {
return () => {
let resultSoFar = this.resolve();
while (resultSoFar && isReferenceType(resultSoFar)) {
resultSoFar = (resultSoFar as any).getTarget?.();
}
return !!resultSoFar;
};
}
if (propName === 'getTarget') {
return () => {
return this.resolve();
};
}
if (propName === 'tableProvider') {
return this.tableProvider;
}
if (propName === 'isEqual') {
//Need to be able to check equality without resolution, because resolution need to check equality
//To see if you need to make a UnionType
return (targetType: BscType, data?: TypeCompatibilityData) => {
if (!targetType) {
return false;
}
const resolvedType = this.resolve();
let equal = false;
if (resolvedType && !isReferenceType(resolvedType)) {
equal = resolvedType.isEqual(targetType, data);
} else if (isReferenceType(targetType)) {
equal = this.fullName.toLowerCase() === targetType.fullName.toLowerCase() &&
(this.tableProvider === targetType.tableProvider ||
this.tableProvider().name === targetType.tableProvider().name);
} else {
equal = targetType.isEqual(this, data);
}
return equal;
};
}
if (propName === 'isTypeCompatible') {
//Need to be able to check equality without resolution, because resolution need to check equality
//To see if you need to make a UnionType
return (targetType: BscType, data?: TypeCompatibilityData) => {
if (!targetType) {
return false;
}
if (isDynamicType(targetType)) {
return true;
}
const resolvedType = this.resolve();
if (resolvedType && !isReferenceType(resolvedType)) {
return resolvedType.isTypeCompatible(targetType, data);
} else if (isReferenceType(targetType)) {
return this.fullName.toLowerCase() === targetType.fullName.toLowerCase() &&
this.tableProvider === targetType.tableProvider;
}
return targetType.isTypeCompatible(this, data);
};
}
//There may be some need to specifically get members on ReferenceType in the future
// eg: if (Reflect.has(target, name)) {
// return Reflect.get(target, propName, receiver);
let innerType = this.resolve();
if (!innerType) {
// No real BscType found - we may need to handle some specific cases
if (propName === 'getMemberType') {
// We're looking for a member of a reference type
// Since we don't know what type this is, yet, return ReferenceType
return (memberName: string, options: GetTypeOptions) => {
const resolvedType = this.resolve();
if (resolvedType) {
return resolvedType.getMemberType(memberName, options);
}
const refLookUp = `${memberName.toLowerCase()}-${options?.flags}`;
let memberTypeReference = this.memberTypeReferences.get(refLookUp);
if (memberTypeReference) {
return memberTypeReference;
}
memberTypeReference = new ReferenceType(memberName, this.makeMemberFullName(memberName), options.flags, this.futureMemberTableProvider);
this.memberTypeReferences.set(refLookUp, memberTypeReference);
return memberTypeReference;
};
} else if (propName === 'getCallFuncType') {
// We're looking for a callfunc member of a reference type
// Since we don't know what type this is, yet, return ReferenceType
return (memberName: string, options: GetTypeOptions) => {
const resolvedType = this.resolve();
if (isComponentType(resolvedType)) {
return resolvedType.getCallFuncType(memberName, options);
}
const refLookUp = `${memberName.toLowerCase()}-${options.flags}-callfunc`;
let callFuncMemberTypeReference = this.callFuncMemberTypeReferences.get(refLookUp);
if (callFuncMemberTypeReference) {
return callFuncMemberTypeReference;
}
callFuncMemberTypeReference = new ReferenceType(memberName, this.makeMemberFullName(memberName), options.flags, this.futureCallFuncMemberTableProvider);
this.callFuncMemberTypeReferences.set(refLookUp, callFuncMemberTypeReference);
return callFuncMemberTypeReference;
};
} else if (propName === 'toString') {
// This type was never found
// For diagnostics, we should return the expected name of of the type
return () => this.fullName;
} else if (propName === 'toTypeString') {
// For transpilation, we should 'dynamic'
return () => 'dynamic';
} else if (propName === 'returnType') {
let propRefType = this.propertyTypeReference.get(propName);
if (!propRefType) {
propRefType = new TypePropertyReferenceType(this, propName);
this.propertyTypeReference.set(propName, propRefType);
}
return propRefType;
} else if (propName === 'memberTable') {
return this.memberTable;
} else if (propName === 'getMemberTable') {
return () => {
return this.memberTable;
};
} else if (propName === 'callFuncTable') {
return (this as any).callFuncMemberTable;
} else if (propName === 'getCallFuncTable') {
return () => {
return (this as any).callFuncMemberTable;
};
} else if (propName === 'isTypeCompatible') {
return (targetType: BscType) => {
return isDynamicType(targetType);
};
} else if (propName === 'isEqual') {
return (targetType: BscType) => {
if (isReferenceType(targetType)) {
return this.fullName.toLowerCase() === targetType.toString().toLowerCase() &&
this.tableProvider() === targetType.tableProvider();
}
return false;
};
} else if (propName === 'addBuiltInInterfaces') {
// this is an unknown type. There is no use in adding built in interfaces
// return no-op function
return () => { };
} else if (propName === 'hasAddedBuiltInInterfaces') {
// this is an unknown type. There is no use in adding built in interfaces
return true;
} else if (propName === 'isBuiltIn') {
return false;
}
}
if (!innerType) {
innerType = DynamicType.instance;
}
const result = Reflect.get(innerType, propName, innerType);
return result;
},
set: (target, name, value, receiver) => {
//There may be some need to specifically set members on ReferenceType in the future
// eg: if (Reflect.has(target, name)) {
// return Reflect.set(target, name, value, receiver);
let innerType = this.resolve();
// Look for circular references
if (!innerType || this.referenceChain.has(innerType)) {
if (name === 'hasAddedBuiltInInterfaces') {
// ignore this
return true;
}
console.log(`Proxy set error`, name, value, innerType);
const error = new Error(`Reference Type Proxy set error: ${{ memberKey: memberKey, name: name, value: value }}`);
console.log(error.stack);
return false;
}
const result = Reflect.set(innerType, name, value, innerType);
this.referenceChain.clear();
return result;
}
});
}
public readonly kind = BscTypeKind.ReferenceType;
getTarget() {
return this.resolve();
}
/**
* Resolves the type based on the original name and the table provider
*/
private resolve(): BscType {
const symbolTable = this.tableProvider();
if (!symbolTable) {
return;
}
// Look for circular references
let resolvedType = symbolTable.getSymbolType(this.memberKey, { flags: this.flags, onlyCacheResolvedTypes: true });
if (!resolvedType) {
// could not find this member
return;
}
if (isAnyReferenceType(resolvedType)) {
// If this is a referenceType, keep digging down until we have a non reference Type.
while (resolvedType && isAnyReferenceType(resolvedType)) {
if (this.referenceChain.has(resolvedType)) {
// this is a circular reference
this.circRefCount++;
}
if (this.circRefCount > 1) {
//It is possible that we could properly resolve the case that one reference points to itself
//see test: '[Scope][symbolTable lookups with enhanced typing][finds correct class field type with default value enums are used]
return;
}
this.referenceChain.add(resolvedType);
resolvedType = (resolvedType as any)?.getTarget?.();
}
this.tableProvider().setCachedType(this.memberKey, { type: resolvedType }, { flags: this.flags });
}
if (resolvedType && !isAnyReferenceType(resolvedType)) {
this.circRefCount = 0;
this.referenceChain.clear();
}
return resolvedType;
}
get __reflection() {
return { name: 'ReferenceType' };
}
makeMemberFullName(memberName: string) {
return this.fullName + '.' + memberName;
}
private circRefCount = 0;
private referenceChain = new Set<BscType>();
private propertyTypeReference = new Map<string, TypePropertyReferenceType>();
private memberTypeReferences = new Map<string, ReferenceType>();
private callFuncMemberTypeReferences = new Map<string, ReferenceType>();
private futureMemberTableProvider = () => {
return {
name: `FutureMemberTableProvider: '${this.__identifier}'`,
getSymbolType: (innerName: string, innerOptions: GetTypeOptions) => {
const resolvedType = this.resolve();
if (resolvedType) {
return resolvedType.getMemberType(innerName, innerOptions);
}
},
setCachedType: (innerName: string, innerResolvedTypeCacheEntry: TypeChainEntry, options: GetSymbolTypeOptions) => {
const resolvedType = this.resolve();
if (resolvedType) {
resolvedType.memberTable.setCachedType(innerName, innerResolvedTypeCacheEntry, options);
}
},
addSibling: (symbolTable: SymbolTable) => {
const resolvedType = this.resolve();
if (resolvedType) {
resolvedType.memberTable?.addSibling?.(symbolTable);
}
}
};
};
private futureCallFuncMemberTableProvider = () => {
return {
name: `FutureCallFuncMemberTableProvider: '${this.__identifier}'`,
getSymbolType: (innerName: string, innerOptions: GetTypeOptions) => {
const resolvedType = this.resolve();
if (isComponentType(resolvedType)) {
return resolvedType.getCallFuncType(innerName, innerOptions);
}
},
setCachedType: (innerName: string, innerResolvedTypeCacheEntry: TypeChainEntry, options: GetSymbolTypeOptions) => {
const resolvedType = this.resolve();
if (isComponentType(resolvedType)) {
resolvedType.getCallFuncTable().setCachedType(innerName, innerResolvedTypeCacheEntry, options);
}
},
addSibling: (symbolTable: SymbolTable) => {
const resolvedType = this.resolve();
if (isComponentType(resolvedType)) {
resolvedType.getCallFuncTable()?.addSibling?.(symbolTable);
}
}
};
};
}
/**
* Use this class for when you need to reference a property of a BscType, and that property is also a BscType,
* Especially when the instance with the property may not have been instantiated/discovered yet
*
* For Example, FunctionType.returnType --- if the FunctionExpression has not been walked yet, it will be a ReferenceType
* if we just access .returnType on a ReferenceType that doesn't have access to an actual FunctionType yet, .returnType will be undefined
* So when we use this class, it maintains that reference to a potential ReferenceType, and this class will change from
* returning undefined to the ACTUAL .returnType when the ReferenceType can be resolved.
*
* This is really cool. It's like programming with time-travel.
*/
export class TypePropertyReferenceType extends BscType {
constructor(public outerType: BscType, public propertyName: string) {
super(propertyName);
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get: (target, propName, receiver) => {
if (propName === '__reflection') {
// Cheeky way to get `isTypePropertyReferenceType` reflection to work
return { name: 'TypePropertyReferenceType' };
}
if (propName === 'outerType') {
return outerType;
}
if (propName === 'getTarget') {
return () => {
return this.getTarget();
};
}
if (propName === 'isResolvable') {
return () => {
return !!(isAnyReferenceType(this.outerType) ? (this.outerType as any).getTarget() : this.outerType?.isResolvable());
};
}
if (isAnyReferenceType(this.outerType) && !this.outerType.isResolvable()) {
if (propName === 'getMemberType') {
//If we're calling `getMemberType()`, we need it to proxy to using the actual symbol table
//So if that symbol is ever populated, the correct type is passed through
return (memberName: string, options: GetSymbolTypeOptions) => {
const fullMemberName = this.outerType.toString() + '.' + memberName;
return new ReferenceType(memberName, fullMemberName, options.flags, () => {
return {
name: `TypePropertyReferenceType : '${fullMemberName}'`,
getSymbolType: (innerName: string, innerOptions: GetTypeOptions) => {
return this.outerType?.[this.propertyName]?.getMemberType(innerName, innerOptions);
},
setCachedType: (innerName: string, innerTypeCacheEntry: TypeChainEntry, innerOptions: GetTypeOptions) => {
return this.outerType?.[this.propertyName]?.memberTable.setCachedType(innerName, innerTypeCacheEntry, innerOptions);
},
addSibling: (symbolTable: SymbolTable) => {
return this.outerType?.[this.propertyName]?.memberTable?.addSibling?.(symbolTable);
}
};
});
};
}
if (propName === 'isResolvable') {
return () => false;
}
}
let inner = this.getTarget();
if (!inner) {
inner = DynamicType.instance;
}
if (inner) {
const result = Reflect.get(inner, propName, inner);
return result;
}
},
set: (target, name, value, receiver) => {
//There may be some need to specifically set members on ReferenceType in the future
// eg: if (Reflect.has(target, name)) {
// return Reflect.set(target, name, value, receiver);
let inner = this.outerType[this.propertyName];
if (inner) {
const result = Reflect.set(inner, name, value, inner);
return result;
}
}
});
}
getTarget(): BscType {
let actualOuterType = this.outerType;
if (isAnyReferenceType(this.outerType)) {
if ((this.outerType as ReferenceType).isResolvable()) {
actualOuterType = (this.outerType as ReferenceType)?.getTarget();
}
}
return actualOuterType?.[this.propertyName];
}
tableProvider: SymbolTableProvider;
}
/**
* Use this class for when there is a binary operator and either the left hand side and/or the right hand side
* are ReferenceTypes
*/
export class BinaryOperatorReferenceType extends BscType {
cachedType: BscType;
constructor(public leftType: BscType, public operator: Token, public rightType: BscType, binaryOpResolver: (lType: BscType, operator: Token, rType: BscType) => BscType) {
super(operator.text);
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get: (target, propName, receiver) => {
if (propName === '__reflection') {
// Cheeky way to get `BinaryOperatorReferenceType` reflection to work
return { name: 'BinaryOperatorReferenceType' };
}
if (propName === 'leftType') {
return leftType;
}
if (propName === 'rightType') {
return rightType;
}
let resultType: BscType = this.cachedType ?? DynamicType.instance;
if (!this.cachedType) {
if ((isAnyReferenceType(this.leftType) && !this.leftType.isResolvable()) ||
(isAnyReferenceType(this.rightType) && !this.rightType.isResolvable())
) {
if (propName === 'isResolvable') {
return () => false;
}
if (propName === 'getTarget') {
return () => undefined;
}
} else {
resultType = binaryOpResolver(this.leftType, this.operator, this.rightType) ?? DynamicType.instance;
this.cachedType = resultType;
}
}
if (resultType) {
const result = Reflect.get(resultType, propName, resultType);
return result;
}
}
});
}
getTarget: () => BscType;
tableProvider: SymbolTableProvider;
}
/**
* Use this class for when there is a presumable array type that is a referenceType
*/
export class ArrayDefaultTypeReferenceType extends BscType {
constructor(public objType: BscType) {
super('ArrayDefaultType');
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get: (target, propName, receiver) => {
if (propName === '__reflection') {
// Cheeky way to get `ArrayDefaultTypeReferenceType` reflection to work
return { name: 'ArrayDefaultTypeReferenceType' };
}
if (propName === 'objType') {
return objType;
}
if (propName === 'getTarget') {
return () => {
if (isReferenceType(this.objType)) {
(this.objType as any).getTarget();
}
return this.objType;
};
}
let resultType: BscType = DynamicType.instance;
if ((isAnyReferenceType(this.objType) && !this.objType.isResolvable())
) {
if (propName === 'isResolvable') {
return () => false;
}
if (propName === 'getTarget') {
return () => undefined;
}
} else {
if (isArrayType(this.objType)) {
resultType = this.objType.defaultType;
} else {
resultType = DynamicType.instance;
}
}
if (resultType) {
const result = Reflect.get(resultType, propName, resultType);
return result;
}
}
});
}
getTarget: () => BscType;
tableProvider: SymbolTableProvider;
}
/**
* Used for when a Param's initial value is unresolved
* Generally, this will be resolved later *except* for Enum
* Enum Types will have an initial value of an EnumMemberType, but the param
* should be an EnumType
*/
export class ParamTypeFromValueReferenceType extends BscType {
cachedType: BscType;
constructor(public objType: BscType) {
super('ParamInitialValueType');
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
get: (target, propName, receiver) => {
if (propName === '__reflection') {
// Cheeky way to get reflection to work
return { name: 'ParamTypeFromValueReferenceType' };
}
if (propName === 'objType') {
return objType;
}
if (propName === 'getTarget') {
return () => {
if (this.cachedType) {
return this.cachedType;
}
if (isReferenceType(this.objType)) {
(this.objType as any).getTarget();
}
return this.objType;
};
}
let resultType: BscType = this.cachedType ?? DynamicType.instance;
if (!this.cachedType) {
if ((isAnyReferenceType(this.objType) && !this.objType.isResolvable())
) {
if (propName === 'isResolvable') {
return () => false;
}
if (propName === 'getTarget') {
return () => undefined;
}
} else {
resultType = util.getDefaultTypeFromValueType(this.objType);
this.cachedType = resultType;
}
}
if (resultType) {
const result = Reflect.get(resultType, propName, resultType);
return result;
}
}
});
}
}
/**
* Gives an array of all the symbol names that need to be resolved to make the given reference type be resolved
*/
export function getAllRequiredSymbolNames(refType: BscType, namespaceLower?: string): Array<{ name: string; namespacedName?: string }> {
if (refType.isResolvable()) {
return [];
}
if (isReferenceType(refType)) {
return [{ name: refType.fullName, namespacedName: namespaceLower ? `${namespaceLower}.${refType.fullName}` : null }];
}
if (isTypePropertyReferenceType(refType)) {
const outer = refType.outerType;
if (isAnyReferenceType(outer)) {
return getAllRequiredSymbolNames(outer);
} else {
return [];
}
}
if (isArrayDefaultTypeReferenceType(refType)) {
const objType = refType.objType;
if (isAnyReferenceType(objType)) {
return getAllRequiredSymbolNames(objType);
} else {
return [];
}
}
if (isBinaryOperatorReferenceType(refType)) {
const left = refType.leftType;
const right = refType.rightType;
const result = [];
if (isAnyReferenceType(left)) {
result.push(...getAllRequiredSymbolNames(left, namespaceLower));
}
if (isAnyReferenceType(right)) {
result.push(...getAllRequiredSymbolNames(right, namespaceLower));
}
return result;
}
if (isParamTypeFromValueReferenceType(refType)) {
const objType = refType.objType;
if (isAnyReferenceType(objType)) {
return getAllRequiredSymbolNames(objType);
} else {
return [];
}
}
return [];
}