-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
CallExpressionBase.ts
167 lines (159 loc) · 5.05 KB
/
CallExpressionBase.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
import { EMPTY_ARRAY, EMPTY_SET } from '../../../utils/blank';
import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { HasEffectsContext } from '../../ExecutionContext';
import type { NodeInteraction, NodeInteractionCalled } from '../../NodeInteractions';
import { INTERACTION_ASSIGNED, INTERACTION_CALLED } from '../../NodeInteractions';
import { type ObjectPath, type PathTracker, UNKNOWN_PATH } from '../../utils/PathTracker';
import {
type ExpressionEntity,
type LiteralValueOrUnknown,
UNKNOWN_EXPRESSION,
UNKNOWN_RETURN_EXPRESSION,
UnknownValue
} from './Expression';
import { NodeBase } from './Node';
export default abstract class CallExpressionBase extends NodeBase implements DeoptimizableEntity {
protected declare interaction: NodeInteractionCalled;
protected returnExpression: [expression: ExpressionEntity, isPure: boolean] | null = null;
private deoptimizableDependentExpressions: DeoptimizableEntity[] = [];
private expressionsToBeDeoptimized = new Set<ExpressionEntity>();
deoptimizeArgumentsOnInteractionAtPath(
interaction: NodeInteraction,
path: ObjectPath,
recursionTracker: PathTracker
): void {
const { args } = interaction;
const [returnExpression, isPure] = this.getReturnExpression(recursionTracker);
if (isPure) return;
const deoptimizedExpressions = args.filter(
expression => !!expression && expression !== UNKNOWN_EXPRESSION
) as ExpressionEntity[];
if (deoptimizedExpressions.length === 0) return;
if (returnExpression === UNKNOWN_EXPRESSION) {
for (const expression of deoptimizedExpressions) {
expression.deoptimizePath(UNKNOWN_PATH);
}
} else {
recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
for (const expression of deoptimizedExpressions) {
this.expressionsToBeDeoptimized.add(expression);
}
returnExpression.deoptimizeArgumentsOnInteractionAtPath(
interaction,
path,
recursionTracker
);
},
null
);
}
}
deoptimizeCache(): void {
if (this.returnExpression?.[0] !== UNKNOWN_EXPRESSION) {
this.returnExpression = UNKNOWN_RETURN_EXPRESSION;
const { deoptimizableDependentExpressions, expressionsToBeDeoptimized } = this;
this.expressionsToBeDeoptimized = EMPTY_SET;
this.deoptimizableDependentExpressions = EMPTY_ARRAY as unknown as DeoptimizableEntity[];
for (const expression of deoptimizableDependentExpressions) {
expression.deoptimizeCache();
}
for (const expression of expressionsToBeDeoptimized) {
expression.deoptimizePath(UNKNOWN_PATH);
}
}
}
deoptimizePath(path: ObjectPath): void {
if (
path.length === 0 ||
this.context.deoptimizationTracker.trackEntityAtPathAndGetIfTracked(path, this)
) {
return;
}
const [returnExpression] = this.getReturnExpression();
if (returnExpression !== UNKNOWN_EXPRESSION) {
returnExpression.deoptimizePath(path);
}
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
const [returnExpression] = this.getReturnExpression(recursionTracker);
if (returnExpression === UNKNOWN_EXPRESSION) {
return UnknownValue;
}
return recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.deoptimizableDependentExpressions.push(origin);
return returnExpression.getLiteralValueAtPath(path, recursionTracker, origin);
},
UnknownValue
);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
interaction: NodeInteractionCalled,
recursionTracker: PathTracker,
origin: DeoptimizableEntity
): [expression: ExpressionEntity, isPure: boolean] {
const returnExpression = this.getReturnExpression(recursionTracker);
if (returnExpression[0] === UNKNOWN_EXPRESSION) {
return returnExpression;
}
return recursionTracker.withTrackedEntityAtPath(
path,
returnExpression,
() => {
this.deoptimizableDependentExpressions.push(origin);
const [expression, isPure] = returnExpression[0].getReturnExpressionWhenCalledAtPath(
path,
interaction,
recursionTracker,
origin
);
return [expression, isPure || returnExpression[1]];
},
UNKNOWN_RETURN_EXPRESSION
);
}
hasEffectsOnInteractionAtPath(
path: ObjectPath,
interaction: NodeInteraction,
context: HasEffectsContext
): boolean {
const { type } = interaction;
if (type === INTERACTION_CALLED) {
const { args, withNew } = interaction;
if (
(withNew ? context.instantiated : context.called).trackEntityAtPathAndGetIfTracked(
path,
args,
this
)
) {
return false;
}
} else if (
(type === INTERACTION_ASSIGNED
? context.assigned
: context.accessed
).trackEntityAtPathAndGetIfTracked(path, this)
) {
return false;
}
const [returnExpression, isPure] = this.getReturnExpression();
return (
(type === INTERACTION_ASSIGNED || !isPure) &&
returnExpression.hasEffectsOnInteractionAtPath(path, interaction, context)
);
}
protected abstract getReturnExpression(
recursionTracker?: PathTracker
): [expression: ExpressionEntity, isPure: boolean];
}