import {HIRFunction, IdentifierId} from '../HIR';
export function outlineFunctions(
fn: HIRFunction,
fbtOperands: Set<IdentifierId>,
): void {
for (const [, block] of fn.body.blocks) {
for (const instr of block.instructions) {
const {value, lvalue} = instr;
if (
value.kind === 'FunctionExpression' ||
value.kind === 'ObjectMethod'
) {
outlineFunctions(value.loweredFunc.func, fbtOperands);
}
if (
value.kind === 'FunctionExpression' &&
value.loweredFunc.dependencies.length === 0 &&
value.loweredFunc.func.context.length === 0 &&
value.loweredFunc.func.id === null &&
!fbtOperands.has(lvalue.identifier.id)
) {
const loweredFunc = value.loweredFunc.func;
const id = fn.env.generateGloballyUniqueIdentifierName(loweredFunc.id);
loweredFunc.id = id.value;
fn.env.outlineFunction(loweredFunc, null);
instr.value = {
kind: 'LoadGlobal',
binding: {
kind: 'Global',
name: id.value,
},
loc: value.loc,
};
}
}
}
}