Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monomorphic flow nodes #57977

Merged
merged 17 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 38 additions & 33 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ import {
Expression,
ExpressionStatement,
findAncestor,
FlowArrayMutation,
FlowAssignment,
FlowCall,
FlowCondition,
FlowFlags,
FlowLabel,
FlowNode,
FlowReduceLabel,
FlowSwitchClause,
forEach,
forEachChild,
ForInOrOfStatement,
Expand Down Expand Up @@ -494,9 +499,9 @@ export const enum ContainerFlags {
IsObjectLiteralOrClassExpressionMethodOrAccessor = 1 << 7,
}

function initFlowNode<T extends FlowNode>(node: T) {
Debug.attachFlowNodeDebugInfo(node);
return node;
/** @internal */
export function createFlowNode(flags: FlowFlags, node: unknown, antecedent: FlowNode | FlowNode[] | undefined): FlowNode {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
return Debug.attachFlowNodeDebugInfo({ flags, id: 0, node, antecedent } as FlowNode);
}

const binder = /* @__PURE__ */ createBinder();
Expand Down Expand Up @@ -557,8 +562,8 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
var Symbol: new (flags: SymbolFlags, name: __String) => Symbol;
var classifiableNames: Set<__String>;

var unreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
var reportedUnreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
var unreachableFlow = createFlowNode(FlowFlags.Unreachable, /*node*/ undefined, /*antecedent*/ undefined);
var reportedUnreachableFlow = createFlowNode(FlowFlags.Unreachable, /*node*/ undefined, /*antecedent*/ undefined);
var bindBinaryExpressionFlow = createBindBinaryExpressionFlow();
/* eslint-enable no-var */

Expand Down Expand Up @@ -1013,7 +1018,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
// A non-async, non-generator IIFE is considered part of the containing control flow. Return statements behave
// similarly to break statements that exit to a label just past the statement body.
if (!isImmediatelyInvoked) {
currentFlow = initFlowNode({ flags: FlowFlags.Start });
currentFlow = createFlowNode(FlowFlags.Start, /*node*/ undefined, /*antecedent*/ undefined);
if (containerFlags & (ContainerFlags.IsFunctionExpression | ContainerFlags.IsObjectLiteralOrClassExpressionMethodOrAccessor)) {
currentFlow.node = node as FunctionExpression | ArrowFunction | MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration;
}
Expand Down Expand Up @@ -1332,16 +1337,16 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
return containsNarrowableReference(expr);
}

function createBranchLabel(): FlowLabel {
return initFlowNode({ flags: FlowFlags.BranchLabel, antecedents: undefined });
function createBranchLabel() {
return createFlowNode(FlowFlags.BranchLabel, /*node*/ undefined, /*antecedent*/ undefined) as FlowLabel;
}

function createLoopLabel(): FlowLabel {
return initFlowNode({ flags: FlowFlags.LoopLabel, antecedents: undefined });
function createLoopLabel() {
return createFlowNode(FlowFlags.LoopLabel, /*node*/ undefined, /*antecedent*/ undefined) as FlowLabel;
}

function createReduceLabel(target: FlowLabel, antecedents: FlowNode[], antecedent: FlowNode): FlowReduceLabel {
return initFlowNode({ flags: FlowFlags.ReduceLabel, target, antecedents, antecedent });
function createReduceLabel(target: FlowLabel, antecedents: FlowNode[], antecedent: FlowNode) {
return createFlowNode(FlowFlags.ReduceLabel, { target, antecedents }, antecedent) as FlowReduceLabel;
}

function setFlowNodeReferenced(flow: FlowNode) {
Expand All @@ -1350,13 +1355,13 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
}

function addAntecedent(label: FlowLabel, antecedent: FlowNode): void {
if (!(antecedent.flags & FlowFlags.Unreachable) && !contains(label.antecedents, antecedent)) {
(label.antecedents || (label.antecedents = [])).push(antecedent);
if (!(antecedent.flags & FlowFlags.Unreachable) && !contains(label.antecedent, antecedent)) {
(label.antecedent || (label.antecedent = [])).push(antecedent);
setFlowNodeReferenced(antecedent);
}
}

function createFlowCondition(flags: FlowFlags, antecedent: FlowNode, expression: Expression | undefined): FlowNode {
function createFlowCondition(flags: FlowFlags.TrueCondition | FlowFlags.FalseCondition, antecedent: FlowNode, expression: Expression | undefined) {
if (antecedent.flags & FlowFlags.Unreachable) {
return antecedent;
}
Expand All @@ -1374,32 +1379,32 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
return antecedent;
}
setFlowNodeReferenced(antecedent);
return initFlowNode({ flags, antecedent, node: expression });
return createFlowNode(flags, expression, antecedent) as FlowCondition;
}

function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): FlowNode {
function createFlowSwitchClause(antecedent: FlowNode, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) {
setFlowNodeReferenced(antecedent);
return initFlowNode({ flags: FlowFlags.SwitchClause, antecedent, switchStatement, clauseStart, clauseEnd });
return createFlowNode(FlowFlags.SwitchClause, { switchStatement, clauseStart, clauseEnd }, antecedent) as FlowSwitchClause;
}

function createFlowMutation(flags: FlowFlags, antecedent: FlowNode, node: Expression | VariableDeclaration | ArrayBindingElement): FlowNode {
function createFlowMutation(flags: FlowFlags.Assignment | FlowFlags.ArrayMutation, antecedent: FlowNode, node: Expression | VariableDeclaration | ArrayBindingElement) {
setFlowNodeReferenced(antecedent);
hasFlowEffects = true;
const result = initFlowNode({ flags, antecedent, node });
const result = createFlowNode(flags, node, antecedent) as FlowAssignment | FlowArrayMutation;
if (currentExceptionTarget) {
addAntecedent(currentExceptionTarget, result);
}
return result;
}

function createFlowCall(antecedent: FlowNode, node: CallExpression): FlowNode {
function createFlowCall(antecedent: FlowNode, node: CallExpression) {
setFlowNodeReferenced(antecedent);
hasFlowEffects = true;
return initFlowNode({ flags: FlowFlags.Call, antecedent, node });
return createFlowNode(FlowFlags.Call, node, antecedent) as FlowCall;
}

function finishFlowLabel(flow: FlowLabel): FlowNode {
const antecedents = flow.antecedents;
const antecedents = flow.antecedent;
if (!antecedents) {
return unreachableFlow;
}
Expand Down Expand Up @@ -1658,7 +1663,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
// set of antecedents for the pre-finally label. As control flow analysis passes by a ReduceLabel
// node, the pre-finally label is temporarily switched to the reduced antecedent set.
const finallyLabel = createBranchLabel();
finallyLabel.antecedents = concatenate(concatenate(normalExitLabel.antecedents, exceptionLabel.antecedents), returnLabel.antecedents);
finallyLabel.antecedent = concatenate(concatenate(normalExitLabel.antecedent, exceptionLabel.antecedent), returnLabel.antecedent);
currentFlow = finallyLabel;
bind(node.finallyBlock);
if (currentFlow.flags & FlowFlags.Unreachable) {
Expand All @@ -1668,18 +1673,18 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
else {
// If we have an IIFE return target and return statements in the try or catch blocks, add a control
// flow that goes back through the finally block and back through only the return statements.
if (currentReturnTarget && returnLabel.antecedents) {
addAntecedent(currentReturnTarget, createReduceLabel(finallyLabel, returnLabel.antecedents, currentFlow));
if (currentReturnTarget && returnLabel.antecedent) {
addAntecedent(currentReturnTarget, createReduceLabel(finallyLabel, returnLabel.antecedent, currentFlow));
}
// If we have an outer exception target (i.e. a containing try-finally or try-catch-finally), add a
// control flow that goes back through the finally blok and back through each possible exception source.
if (currentExceptionTarget && exceptionLabel.antecedents) {
addAntecedent(currentExceptionTarget, createReduceLabel(finallyLabel, exceptionLabel.antecedents, currentFlow));
if (currentExceptionTarget && exceptionLabel.antecedent) {
addAntecedent(currentExceptionTarget, createReduceLabel(finallyLabel, exceptionLabel.antecedent, currentFlow));
}
// If the end of the finally block is reachable, but the end of the try and catch blocks are not,
// convert the current flow to unreachable. For example, 'try { return 1; } finally { ... }' should
// result in an unreachable current control flow.
currentFlow = normalExitLabel.antecedents ? createReduceLabel(finallyLabel, normalExitLabel.antecedents, currentFlow) : unreachableFlow;
currentFlow = normalExitLabel.antecedent ? createReduceLabel(finallyLabel, normalExitLabel.antecedent, currentFlow) : unreachableFlow;
}
}
else {
Expand All @@ -1700,7 +1705,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
// We mark a switch statement as possibly exhaustive if it has no default clause and if all
// case clauses have unreachable end points (e.g. they all return). Note, we no longer need
// this property in control flow analysis, it's there only for backwards compatibility.
node.possiblyExhaustive = !hasDefault && !postSwitchLabel.antecedents;
node.possiblyExhaustive = !hasDefault && !postSwitchLabel.antecedent;
if (!hasDefault) {
addAntecedent(postSwitchLabel, createFlowSwitchClause(preSwitchCaseFlow, node, 0, 0));
}
Expand All @@ -1712,7 +1717,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
function bindCaseBlock(node: CaseBlock): void {
const clauses = node.clauses;
const isNarrowingSwitch = node.parent.expression.kind === SyntaxKind.TrueKeyword || isNarrowingExpression(node.parent.expression);
let fallthroughFlow = unreachableFlow;
let fallthroughFlow: FlowNode = unreachableFlow;

for (let i = 0; i < clauses.length; i++) {
const clauseStart = i;
Expand Down Expand Up @@ -2432,7 +2437,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
const host = typeAlias.parent.parent;
container = (getEnclosingContainer(host) as IsContainer | undefined) || file;
blockScopeContainer = (getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined) || file;
currentFlow = initFlowNode({ flags: FlowFlags.Start });
currentFlow = createFlowNode(FlowFlags.Start, /*node*/ undefined, /*antecedent*/ undefined);
parent = typeAlias;
bind(typeAlias.typeExpression);
const declName = getNameOfDeclaration(typeAlias);
Expand Down Expand Up @@ -2504,7 +2509,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
const enclosingBlockScopeContainer = host ? getEnclosingBlockScopeContainer(host) as IsBlockScopedContainer | undefined : undefined;
container = enclosingContainer || file;
blockScopeContainer = enclosingBlockScopeContainer || file;
currentFlow = initFlowNode({ flags: FlowFlags.Start });
currentFlow = createFlowNode(FlowFlags.Start, /*node*/ undefined, /*antecedent*/ undefined);
parent = jsDocImportTag;
bind(jsDocImportTag.importClause);
}
Expand Down
Loading
Loading