/
LogicalExpression.ts
200 lines (185 loc) · 6.26 KB
/
LogicalExpression.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
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
NodeRenderOptions,
removeLineBreaks,
RenderOptions
} from '../../utils/renderHelpers';
import { removeAnnotations } from '../../utils/treeshakeNode';
import CallOptions from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import {
EMPTY_IMMUTABLE_TRACKER,
ImmutableEntityPathTracker
} from '../utils/ImmutableEntityPathTracker';
import {
EMPTY_PATH,
LiteralValueOrUnknown,
ObjectPath,
UNKNOWN_PATH,
UNKNOWN_VALUE
} from '../values';
import CallExpression from './CallExpression';
import * as NodeType from './NodeType';
import { ExpressionEntity } from './shared/Expression';
import { MultiExpression } from './shared/MultiExpression';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
export type LogicalOperator = '||' | '&&';
export default class LogicalExpression extends NodeBase implements DeoptimizableEntity {
left!: ExpressionNode;
operator!: LogicalOperator;
right!: ExpressionNode;
type!: NodeType.tLogicalExpression;
// We collect deoptimization information if usedBranch !== null
private expressionsToBeDeoptimized: DeoptimizableEntity[] = [];
private isBranchResolutionAnalysed = false;
private unusedBranch: ExpressionNode | null = null;
private usedBranch: ExpressionNode | null = null;
bind() {
super.bind();
if (!this.isBranchResolutionAnalysed) this.analyseBranchResolution();
}
deoptimizeCache() {
if (this.usedBranch !== null) {
// We did not track if there were reassignments to any of the branches.
// Also, the return values might need reassignment.
this.usedBranch = null;
(this.unusedBranch as ExpressionNode).deoptimizePath(UNKNOWN_PATH);
for (const expression of this.expressionsToBeDeoptimized) {
expression.deoptimizeCache();
}
}
}
deoptimizePath(path: ObjectPath) {
if (path.length > 0) {
if (!this.isBranchResolutionAnalysed) this.analyseBranchResolution();
if (this.usedBranch === null) {
this.left.deoptimizePath(path);
this.right.deoptimizePath(path);
} else {
this.usedBranch.deoptimizePath(path);
}
}
}
getLiteralValueAtPath(
path: ObjectPath,
recursionTracker: ImmutableEntityPathTracker,
origin: DeoptimizableEntity
): LiteralValueOrUnknown {
if (!this.isBranchResolutionAnalysed) this.analyseBranchResolution();
if (this.usedBranch === null) return UNKNOWN_VALUE;
this.expressionsToBeDeoptimized.push(origin);
return this.usedBranch.getLiteralValueAtPath(path, recursionTracker, origin);
}
getReturnExpressionWhenCalledAtPath(
path: ObjectPath,
recursionTracker: ImmutableEntityPathTracker,
origin: DeoptimizableEntity
): ExpressionEntity {
if (!this.isBranchResolutionAnalysed) this.analyseBranchResolution();
if (this.usedBranch === null)
return new MultiExpression([
this.left.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin),
this.right.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin)
]);
this.expressionsToBeDeoptimized.push(origin);
return this.usedBranch.getReturnExpressionWhenCalledAtPath(path, recursionTracker, origin);
}
hasEffects(options: ExecutionPathOptions): boolean {
if (this.usedBranch === null) {
return this.left.hasEffects(options) || this.right.hasEffects(options);
}
return this.usedBranch.hasEffects(options);
}
hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
if (path.length === 0) return false;
if (this.usedBranch === null) {
return (
this.left.hasEffectsWhenAccessedAtPath(path, options) ||
this.right.hasEffectsWhenAccessedAtPath(path, options)
);
}
return this.usedBranch.hasEffectsWhenAccessedAtPath(path, options);
}
hasEffectsWhenAssignedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
if (path.length === 0) return true;
if (this.usedBranch === null) {
return (
this.left.hasEffectsWhenAssignedAtPath(path, options) ||
this.right.hasEffectsWhenAssignedAtPath(path, options)
);
}
return this.usedBranch.hasEffectsWhenAssignedAtPath(path, options);
}
hasEffectsWhenCalledAtPath(
path: ObjectPath,
callOptions: CallOptions,
options: ExecutionPathOptions
): boolean {
if (this.usedBranch === null) {
return (
this.left.hasEffectsWhenCalledAtPath(path, callOptions, options) ||
this.right.hasEffectsWhenCalledAtPath(path, callOptions, options)
);
}
return this.usedBranch.hasEffectsWhenCalledAtPath(path, callOptions, options);
}
include(includeChildrenRecursively: IncludeChildren) {
this.included = true;
if (
includeChildrenRecursively ||
this.usedBranch === null ||
(this.unusedBranch as ExpressionNode).shouldBeIncluded()
) {
this.left.include(includeChildrenRecursively);
this.right.include(includeChildrenRecursively);
} else {
this.usedBranch.include(includeChildrenRecursively);
}
}
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent, preventASI }: NodeRenderOptions = BLANK
) {
if (!this.left.included || !this.right.included) {
const operatorPos = findFirstOccurrenceOutsideComment(
code.original,
this.operator,
this.left.end
);
if (this.right.included) {
code.remove(this.start, operatorPos + 2);
if (preventASI) {
removeLineBreaks(code, operatorPos + 2, this.right.start);
}
} else {
code.remove(operatorPos, this.end);
}
removeAnnotations(this, code);
(this.usedBranch as ExpressionNode).render(code, options, {
isCalleeOfRenderedParent: renderedParentType
? isCalleeOfRenderedParent
: (this.parent as CallExpression).callee === this,
renderedParentType: renderedParentType || this.parent.type
});
} else {
super.render(code, options);
}
}
private analyseBranchResolution() {
this.isBranchResolutionAnalysed = true;
const leftValue = this.left.getLiteralValueAtPath(EMPTY_PATH, EMPTY_IMMUTABLE_TRACKER, this);
if (leftValue !== UNKNOWN_VALUE) {
if (this.operator === '||' ? leftValue : !leftValue) {
this.usedBranch = this.left;
this.unusedBranch = this.right;
} else {
this.usedBranch = this.right;
this.unusedBranch = this.left;
}
}
}
}