import {CompilerError} from '../CompilerError';
import {Environment} from '../HIR';
import {
AbstractValue,
BasicBlock,
BlockId,
CallExpression,
Effect,
FunctionEffect,
GeneratedSource,
HIRFunction,
IdentifierId,
InstructionKind,
InstructionValue,
MethodCall,
Phi,
Place,
SpreadPattern,
Type,
ValueKind,
ValueReason,
isArrayType,
isMutableEffect,
isObjectType,
} from '../HIR/HIR';
import {FunctionSignature} from '../HIR/ObjectShape';
import {
printIdentifier,
printMixedHIR,
printPlace,
printSourceLocation,
} from '../HIR/PrintHIR';
import {
eachCallArgument,
eachInstructionOperand,
eachInstructionValueOperand,
eachPatternOperand,
eachTerminalOperand,
eachTerminalSuccessor,
} from '../HIR/visitors';
import {assertExhaustive} from '../Utils/utils';
import {
inferTerminalFunctionEffects,
inferInstructionFunctionEffects,
raiseFunctionEffectErrors,
} from './InferFunctionEffects';
const UndefinedValue: InstructionValue = {
kind: 'Primitive',
loc: GeneratedSource,
value: undefined,
};
export default function inferReferenceEffects(
fn: HIRFunction,
options: {isFunctionExpression: boolean} = {isFunctionExpression: false},
): void {
const initialState = InferenceState.empty(fn.env);
const value: InstructionValue = {
kind: 'Primitive',
loc: fn.loc,
value: undefined,
};
initialState.initialize(value, {
kind: ValueKind.Frozen,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
for (const ref of fn.context) {
const value: InstructionValue = {
kind: 'ObjectExpression',
properties: [],
loc: ref.loc,
};
initialState.initialize(value, {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set([ref]),
});
initialState.define(ref, value);
}
const paramKind: AbstractValue = options.isFunctionExpression
? {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Frozen,
reason: new Set([ValueReason.ReactiveFunctionArgument]),
context: new Set(),
};
if (fn.fnType === 'Component') {
CompilerError.invariant(fn.params.length <= 2, {
reason:
'Expected React component to have not more than two parameters: one for props and for ref',
description: null,
loc: fn.loc,
suggestions: null,
});
const [props, ref] = fn.params;
let value: InstructionValue;
let place: Place;
if (props) {
inferParam(props, initialState, paramKind);
}
if (ref) {
if (ref.kind === 'Identifier') {
place = ref;
value = {
kind: 'ObjectExpression',
properties: [],
loc: ref.loc,
};
} else {
place = ref.place;
value = {
kind: 'ObjectExpression',
properties: [],
loc: ref.place.loc,
};
}
initialState.initialize(value, {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
initialState.define(place, value);
}
} else {
for (const param of fn.params) {
inferParam(param, initialState, paramKind);
}
}
const statesByBlock: Map<BlockId, InferenceState> = new Map();
const queuedStates: Map<BlockId, InferenceState> = new Map();
function queue(blockId: BlockId, state: InferenceState): void {
let queuedState = queuedStates.get(blockId);
if (queuedState != null) {
state = queuedState.merge(state) ?? queuedState;
queuedStates.set(blockId, state);
} else {
const prevState = statesByBlock.get(blockId);
const nextState = prevState != null ? prevState.merge(state) : state;
if (nextState != null) {
queuedStates.set(blockId, nextState);
}
}
}
queue(fn.body.entry, initialState);
const functionEffects: Array<FunctionEffect> = fn.effects ?? [];
while (queuedStates.size !== 0) {
for (const [blockId, block] of fn.body.blocks) {
const incomingState = queuedStates.get(blockId);
queuedStates.delete(blockId);
if (incomingState == null) {
continue;
}
statesByBlock.set(blockId, incomingState);
const state = incomingState.clone();
inferBlock(fn.env, state, block, functionEffects);
for (const nextBlockId of eachTerminalSuccessor(block.terminal)) {
queue(nextBlockId, state);
}
}
}
if (options.isFunctionExpression) {
fn.effects = functionEffects;
} else {
raiseFunctionEffectErrors(functionEffects);
}
}
type FreezeAction = {values: Set<InstructionValue>; reason: Set<ValueReason>};
class InferenceState {
#env: Environment;
#values: Map<InstructionValue, AbstractValue>;
#variables: Map<IdentifierId, Set<InstructionValue>>;
constructor(
env: Environment,
values: Map<InstructionValue, AbstractValue>,
variables: Map<IdentifierId, Set<InstructionValue>>,
) {
this.#env = env;
this.#values = values;
this.#variables = variables;
}
static empty(env: Environment): InferenceState {
return new InferenceState(env, new Map(), new Map());
}
initialize(value: InstructionValue, kind: AbstractValue): void {
CompilerError.invariant(value.kind !== 'LoadLocal', {
reason:
'Expected all top-level identifiers to be defined as variables, not values',
description: null,
loc: value.loc,
suggestions: null,
});
this.#values.set(value, kind);
}
values(place: Place): Array<InstructionValue> {
const values = this.#variables.get(place.identifier.id);
CompilerError.invariant(values != null, {
reason: `[hoisting] Expected value kind to be initialized`,
description: `${printPlace(place)}`,
loc: place.loc,
suggestions: null,
});
return Array.from(values);
}
kind(place: Place): AbstractValue {
const values = this.#variables.get(place.identifier.id);
CompilerError.invariant(values != null, {
reason: `[hoisting] Expected value kind to be initialized`,
description: `${printPlace(place)}`,
loc: place.loc,
suggestions: null,
});
let mergedKind: AbstractValue | null = null;
for (const value of values) {
const kind = this.#values.get(value)!;
mergedKind =
mergedKind !== null ? mergeAbstractValues(mergedKind, kind) : kind;
}
CompilerError.invariant(mergedKind !== null, {
reason: `InferReferenceEffects::kind: Expected at least one value`,
description: `No value found at \`${printPlace(place)}\``,
loc: place.loc,
suggestions: null,
});
return mergedKind;
}
alias(place: Place, value: Place): void {
const values = this.#variables.get(value.identifier.id);
CompilerError.invariant(values != null, {
reason: `[hoisting] Expected value for identifier to be initialized`,
description: `${printIdentifier(value.identifier)}`,
loc: value.loc,
suggestions: null,
});
this.#variables.set(place.identifier.id, new Set(values));
}
define(place: Place, value: InstructionValue): void {
CompilerError.invariant(this.#values.has(value), {
reason: `Expected value to be initialized at '${printSourceLocation(
value.loc,
)}'`,
description: null,
loc: value.loc,
suggestions: null,
});
this.#variables.set(place.identifier.id, new Set([value]));
}
isDefined(place: Place): boolean {
return this.#variables.has(place.identifier.id);
}
referenceAndRecordEffects(
freezeActions: Array<FreezeAction>,
place: Place,
effectKind: Effect,
reason: ValueReason,
): void {
const values = this.#variables.get(place.identifier.id);
if (values === undefined) {
CompilerError.invariant(effectKind !== Effect.Store, {
reason: '[InferReferenceEffects] Unhandled store reference effect',
description: null,
loc: place.loc,
suggestions: null,
});
place.effect =
effectKind === Effect.ConditionallyMutate
? Effect.ConditionallyMutate
: Effect.Read;
return;
}
const action = this.reference(place, effectKind, reason);
action && freezeActions.push(action);
}
freezeValues(values: Set<InstructionValue>, reason: Set<ValueReason>): void {
for (const value of values) {
this.#values.set(value, {
kind: ValueKind.Frozen,
reason,
context: new Set(),
});
if (value.kind === 'FunctionExpression') {
if (
this.#env.config.enablePreserveExistingMemoizationGuarantees ||
this.#env.config.enableTransitivelyFreezeFunctionExpressions
) {
if (value.kind === 'FunctionExpression') {
for (const operand of eachInstructionValueOperand(value)) {
const operandValues = this.#variables.get(operand.identifier.id);
if (operandValues !== undefined) {
this.freezeValues(operandValues, reason);
}
}
}
}
}
}
}
reference(
place: Place,
effectKind: Effect,
reason: ValueReason,
): null | FreezeAction {
const values = this.#variables.get(place.identifier.id);
CompilerError.invariant(values !== undefined, {
reason: '[InferReferenceEffects] Expected value to be initialized',
description: null,
loc: place.loc,
suggestions: null,
});
let valueKind: AbstractValue | null = this.kind(place);
let effect: Effect | null = null;
let freeze: null | FreezeAction = null;
switch (effectKind) {
case Effect.Freeze: {
if (
valueKind.kind === ValueKind.Mutable ||
valueKind.kind === ValueKind.Context ||
valueKind.kind === ValueKind.MaybeFrozen
) {
const reasonSet = new Set([reason]);
effect = Effect.Freeze;
valueKind = {
kind: ValueKind.Frozen,
reason: reasonSet,
context: new Set(),
};
freeze = {values, reason: reasonSet};
} else {
effect = Effect.Read;
}
break;
}
case Effect.ConditionallyMutate: {
if (
valueKind.kind === ValueKind.Mutable ||
valueKind.kind === ValueKind.Context
) {
effect = Effect.ConditionallyMutate;
} else {
effect = Effect.Read;
}
break;
}
case Effect.Mutate: {
effect = Effect.Mutate;
break;
}
case Effect.Store: {
effect = isObjectType(place.identifier) ? Effect.Store : Effect.Mutate;
break;
}
case Effect.Capture: {
if (
valueKind.kind === ValueKind.Primitive ||
valueKind.kind === ValueKind.Global ||
valueKind.kind === ValueKind.Frozen ||
valueKind.kind === ValueKind.MaybeFrozen
) {
effect = Effect.Read;
} else {
effect = Effect.Capture;
}
break;
}
case Effect.Read: {
effect = Effect.Read;
break;
}
case Effect.Unknown: {
CompilerError.invariant(false, {
reason:
'Unexpected unknown effect, expected to infer a precise effect kind',
description: null,
loc: place.loc,
suggestions: null,
});
}
default: {
assertExhaustive(
effectKind,
`Unexpected reference kind \`${effectKind as any as string}\``,
);
}
}
CompilerError.invariant(effect !== null, {
reason: 'Expected effect to be set',
description: null,
loc: place.loc,
suggestions: null,
});
place.effect = effect;
return freeze;
}
merge(other: InferenceState): InferenceState | null {
let nextValues: Map<InstructionValue, AbstractValue> | null = null;
let nextVariables: Map<IdentifierId, Set<InstructionValue>> | null = null;
for (const [id, thisValue] of this.#values) {
const otherValue = other.#values.get(id);
if (otherValue !== undefined) {
const mergedValue = mergeAbstractValues(thisValue, otherValue);
if (mergedValue !== thisValue) {
nextValues = nextValues ?? new Map(this.#values);
nextValues.set(id, mergedValue);
}
}
}
for (const [id, otherValue] of other.#values) {
if (this.#values.has(id)) {
continue;
}
nextValues = nextValues ?? new Map(this.#values);
nextValues.set(id, otherValue);
}
for (const [id, thisValues] of this.#variables) {
const otherValues = other.#variables.get(id);
if (otherValues !== undefined) {
let mergedValues: Set<InstructionValue> | null = null;
for (const otherValue of otherValues) {
if (!thisValues.has(otherValue)) {
mergedValues = mergedValues ?? new Set(thisValues);
mergedValues.add(otherValue);
}
}
if (mergedValues !== null) {
nextVariables = nextVariables ?? new Map(this.#variables);
nextVariables.set(id, mergedValues);
}
}
}
for (const [id, otherValues] of other.#variables) {
if (this.#variables.has(id)) {
continue;
}
nextVariables = nextVariables ?? new Map(this.#variables);
nextVariables.set(id, new Set(otherValues));
}
if (nextVariables === null && nextValues === null) {
return null;
} else {
return new InferenceState(
this.#env,
nextValues ?? new Map(this.#values),
nextVariables ?? new Map(this.#variables),
);
}
}
clone(): InferenceState {
return new InferenceState(
this.#env,
new Map(this.#values),
new Map(this.#variables),
);
}
debug(): any {
const result: any = {values: {}, variables: {}};
const objects: Map<InstructionValue, number> = new Map();
function identify(value: InstructionValue): number {
let id = objects.get(value);
if (id == null) {
id = objects.size;
objects.set(value, id);
}
return id;
}
for (const [value, kind] of this.#values) {
const id = identify(value);
result.values[id] = {kind, value: printMixedHIR(value)};
}
for (const [variable, values] of this.#variables) {
result.variables[`$${variable}`] = [...values].map(identify);
}
return result;
}
inferPhi(phi: Phi): void {
const values: Set<InstructionValue> = new Set();
for (const [_, operand] of phi.operands) {
const operandValues = this.#variables.get(operand.identifier.id);
if (operandValues === undefined) continue;
for (const v of operandValues) {
values.add(v);
}
}
if (values.size > 0) {
this.#variables.set(phi.place.identifier.id, values);
}
}
}
function inferParam(
param: Place | SpreadPattern,
initialState: InferenceState,
paramKind: AbstractValue,
): void {
let value: InstructionValue;
let place: Place;
if (param.kind === 'Identifier') {
place = param;
value = {
kind: 'Primitive',
loc: param.loc,
value: undefined,
};
} else {
place = param.place;
value = {
kind: 'Primitive',
loc: param.place.loc,
value: undefined,
};
}
initialState.initialize(value, paramKind);
initialState.define(place, value);
}
function mergeValues(a: ValueKind, b: ValueKind): ValueKind {
if (a === b) {
return a;
} else if (a === ValueKind.MaybeFrozen || b === ValueKind.MaybeFrozen) {
return ValueKind.MaybeFrozen;
} else if (a === ValueKind.Mutable || b === ValueKind.Mutable) {
if (a === ValueKind.Frozen || b === ValueKind.Frozen) {
return ValueKind.MaybeFrozen;
} else if (a === ValueKind.Context || b === ValueKind.Context) {
return ValueKind.Context;
} else {
return ValueKind.Mutable;
}
} else if (a === ValueKind.Context || b === ValueKind.Context) {
if (a === ValueKind.Frozen || b === ValueKind.Frozen) {
return ValueKind.MaybeFrozen;
} else {
return ValueKind.Context;
}
} else if (a === ValueKind.Frozen || b === ValueKind.Frozen) {
return ValueKind.Frozen;
} else if (a === ValueKind.Global || b === ValueKind.Global) {
return ValueKind.Global;
} else {
CompilerError.invariant(
a === ValueKind.Primitive && b == ValueKind.Primitive,
{
reason: `Unexpected value kind in mergeValues()`,
description: `Found kinds ${a} and ${b}`,
loc: GeneratedSource,
},
);
return ValueKind.Primitive;
}
}
function isSuperset<T>(a: ReadonlySet<T>, b: ReadonlySet<T>): boolean {
for (const v of b) {
if (!a.has(v)) {
return false;
}
}
return true;
}
function mergeAbstractValues(
a: AbstractValue,
b: AbstractValue,
): AbstractValue {
const kind = mergeValues(a.kind, b.kind);
if (
kind === a.kind &&
kind === b.kind &&
isSuperset(a.reason, b.reason) &&
isSuperset(a.context, b.context)
) {
return a;
}
const reason = new Set(a.reason);
for (const r of b.reason) {
reason.add(r);
}
const context = new Set(a.context);
for (const c of b.context) {
context.add(c);
}
return {kind, reason, context};
}
type Continuation =
| {
kind: 'initialize';
valueKind: AbstractValue;
effect: {kind: Effect; reason: ValueReason} | null;
lvalueEffect?: Effect;
}
| {kind: 'funeffects'};
function inferBlock(
env: Environment,
state: InferenceState,
block: BasicBlock,
functionEffects: Array<FunctionEffect>,
): void {
for (const phi of block.phis) {
state.inferPhi(phi);
}
for (const instr of block.instructions) {
const instrValue = instr.value;
const defaultLvalueEffect = Effect.ConditionallyMutate;
let continuation: Continuation;
const freezeActions: Array<FreezeAction> = [];
switch (instrValue.kind) {
case 'BinaryExpression': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {
kind: Effect.Read,
reason: ValueReason.Other,
},
};
break;
}
case 'ArrayExpression': {
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
continuation = {
kind: 'initialize',
valueKind,
effect: {kind: Effect.Capture, reason: ValueReason.Other},
lvalueEffect: Effect.Store,
};
break;
}
case 'NewExpression': {
const valueKind: AbstractValue = {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
state.referenceAndRecordEffects(
freezeActions,
instrValue.callee,
Effect.Read,
ValueReason.Other,
);
for (const operand of eachCallArgument(instrValue.args)) {
state.referenceAndRecordEffects(
freezeActions,
operand,
Effect.ConditionallyMutate,
ValueReason.Other,
);
}
state.initialize(instrValue, valueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
case 'ObjectExpression': {
const valueKind: AbstractValue = hasContextRefOperand(state, instrValue)
? {
kind: ValueKind.Context,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
for (const property of instrValue.properties) {
switch (property.kind) {
case 'ObjectProperty': {
if (property.key.kind === 'computed') {
state.referenceAndRecordEffects(
freezeActions,
property.key.name,
Effect.Freeze,
ValueReason.Other,
);
}
state.referenceAndRecordEffects(
freezeActions,
property.place,
Effect.Capture,
ValueReason.Other,
);
break;
}
case 'Spread': {
state.referenceAndRecordEffects(
freezeActions,
property.place,
Effect.Capture,
ValueReason.Other,
);
break;
}
default: {
assertExhaustive(
property,
`Unexpected property kind \`${(property as any).kind}\``,
);
}
}
}
state.initialize(instrValue, valueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'UnaryExpression': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {kind: Effect.Read, reason: ValueReason.Other},
};
break;
}
case 'UnsupportedNode': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: null,
};
break;
}
case 'JsxExpression': {
if (instrValue.tag.kind === 'Identifier') {
state.referenceAndRecordEffects(
freezeActions,
instrValue.tag,
Effect.Freeze,
ValueReason.JsxCaptured,
);
}
if (instrValue.children !== null) {
for (const child of instrValue.children) {
state.referenceAndRecordEffects(
freezeActions,
child,
Effect.Freeze,
ValueReason.JsxCaptured,
);
}
}
for (const attr of instrValue.props) {
if (attr.kind === 'JsxSpreadAttribute') {
state.referenceAndRecordEffects(
freezeActions,
attr.argument,
Effect.Freeze,
ValueReason.JsxCaptured,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
attr.place,
Effect.Freeze,
ValueReason.JsxCaptured,
);
}
}
state.initialize(instrValue, {
kind: ValueKind.Frozen,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
case 'JsxFragment': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Frozen,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {
kind: Effect.Freeze,
reason: ValueReason.Other,
},
};
break;
}
case 'TemplateLiteral': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {kind: Effect.Read, reason: ValueReason.Other},
};
break;
}
case 'RegExpLiteral': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {
kind: Effect.ConditionallyMutate,
reason: ValueReason.Other,
},
};
break;
}
case 'MetaProperty': {
if (instrValue.meta !== 'import' || instrValue.property !== 'meta') {
continuation = {kind: 'funeffects'};
break;
}
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Global,
reason: new Set([ValueReason.Global]),
context: new Set(),
},
effect: null,
};
break;
}
case 'LoadGlobal':
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Global,
reason: new Set([ValueReason.Global]),
context: new Set(),
},
effect: null,
};
break;
case 'Debugger':
case 'JSXText':
case 'Primitive': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: null,
};
break;
}
case 'ObjectMethod':
case 'FunctionExpression': {
let hasMutableOperand = false;
const mutableOperands: Array<Place> = [];
for (const operand of eachInstructionOperand(instr)) {
state.referenceAndRecordEffects(
freezeActions,
operand,
operand.effect === Effect.Unknown ? Effect.Read : operand.effect,
ValueReason.Other,
);
if (isMutableEffect(operand.effect, operand.loc)) {
mutableOperands.push(operand);
}
hasMutableOperand ||= isMutableEffect(operand.effect, operand.loc);
}
state.initialize(instrValue, {
kind: hasMutableOperand ? ValueKind.Mutable : ValueKind.Frozen,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'TaggedTemplateExpression': {
const operands = [...eachInstructionValueOperand(instrValue)];
if (operands.length !== 1) {
CompilerError.throwTodo({
reason: 'Support tagged template expressions with interpolations',
loc: instrValue.loc,
});
}
const signature = getFunctionCallSignature(
env,
instrValue.tag.identifier.type,
);
let calleeEffect =
signature?.calleeEffect ?? Effect.ConditionallyMutate;
const returnValueKind: AbstractValue =
signature !== null
? {
kind: signature.returnValueKind,
reason: new Set([
signature.returnValueReason ??
ValueReason.KnownReturnSignature,
]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
state.referenceAndRecordEffects(
freezeActions,
instrValue.tag,
calleeEffect,
ValueReason.Other,
);
state.initialize(instrValue, returnValueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
case 'CallExpression': {
const signature = getFunctionCallSignature(
env,
instrValue.callee.identifier.type,
);
const effects =
signature !== null ? getFunctionEffects(instrValue, signature) : null;
const returnValueKind: AbstractValue =
signature !== null
? {
kind: signature.returnValueKind,
reason: new Set([
signature.returnValueReason ??
ValueReason.KnownReturnSignature,
]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
let hasCaptureArgument = false;
for (let i = 0; i < instrValue.args.length; i++) {
const arg = instrValue.args[i];
const place = arg.kind === 'Identifier' ? arg : arg.place;
if (effects !== null) {
state.referenceAndRecordEffects(
freezeActions,
place,
effects[i],
ValueReason.Other,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
place,
Effect.ConditionallyMutate,
ValueReason.Other,
);
}
hasCaptureArgument ||= place.effect === Effect.Capture;
}
if (signature !== null) {
state.referenceAndRecordEffects(
freezeActions,
instrValue.callee,
signature.calleeEffect,
ValueReason.Other,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
instrValue.callee,
Effect.ConditionallyMutate,
ValueReason.Other,
);
}
hasCaptureArgument ||= instrValue.callee.effect === Effect.Capture;
state.initialize(instrValue, returnValueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = hasCaptureArgument
? Effect.Store
: Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
case 'MethodCall': {
CompilerError.invariant(state.isDefined(instrValue.receiver), {
reason:
'[InferReferenceEffects] Internal error: receiver of PropertyCall should have been defined by corresponding PropertyLoad',
description: null,
loc: instrValue.loc,
suggestions: null,
});
state.referenceAndRecordEffects(
freezeActions,
instrValue.property,
Effect.Read,
ValueReason.Other,
);
const signature = getFunctionCallSignature(
env,
instrValue.property.identifier.type,
);
const returnValueKind: AbstractValue =
signature !== null
? {
kind: signature.returnValueKind,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
if (
signature !== null &&
signature.mutableOnlyIfOperandsAreMutable &&
areArgumentsImmutableAndNonMutating(state, instrValue.args)
) {
for (const arg of instrValue.args) {
const place = arg.kind === 'Identifier' ? arg : arg.place;
state.referenceAndRecordEffects(
freezeActions,
place,
Effect.Read,
ValueReason.Other,
);
}
state.referenceAndRecordEffects(
freezeActions,
instrValue.receiver,
Effect.Capture,
ValueReason.Other,
);
state.initialize(instrValue, returnValueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect =
instrValue.receiver.effect === Effect.Capture
? Effect.Store
: Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
const effects =
signature !== null ? getFunctionEffects(instrValue, signature) : null;
let hasCaptureArgument = false;
for (let i = 0; i < instrValue.args.length; i++) {
const arg = instrValue.args[i];
const place = arg.kind === 'Identifier' ? arg : arg.place;
if (effects !== null) {
state.referenceAndRecordEffects(
freezeActions,
place,
effects[i],
ValueReason.Other,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
place,
Effect.ConditionallyMutate,
ValueReason.Other,
);
}
hasCaptureArgument ||= place.effect === Effect.Capture;
}
if (signature !== null) {
state.referenceAndRecordEffects(
freezeActions,
instrValue.receiver,
signature.calleeEffect,
ValueReason.Other,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
instrValue.receiver,
Effect.ConditionallyMutate,
ValueReason.Other,
);
}
hasCaptureArgument ||= instrValue.receiver.effect === Effect.Capture;
state.initialize(instrValue, returnValueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = hasCaptureArgument
? Effect.Store
: Effect.ConditionallyMutate;
continuation = {kind: 'funeffects'};
break;
}
case 'PropertyStore': {
const effect =
state.kind(instrValue.object).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
effect,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.object,
Effect.Store,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'PropertyDelete': {
continuation = {
kind: 'initialize',
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
effect: {kind: Effect.Mutate, reason: ValueReason.Other},
};
break;
}
case 'PropertyLoad': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.object,
Effect.Read,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
state.initialize(instrValue, state.kind(instrValue.object));
state.define(lvalue, instrValue);
continuation = {kind: 'funeffects'};
break;
}
case 'ComputedStore': {
const effect =
state.kind(instrValue.object).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
effect,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.property,
Effect.Capture,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.object,
Effect.Store,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'ComputedDelete': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.object,
Effect.Mutate,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.property,
Effect.Read,
ValueReason.Other,
);
state.initialize(instrValue, {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.Mutate;
continuation = {kind: 'funeffects'};
break;
}
case 'ComputedLoad': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.object,
Effect.Read,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.property,
Effect.Read,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
state.initialize(instrValue, state.kind(instrValue.object));
state.define(lvalue, instrValue);
continuation = {kind: 'funeffects'};
break;
}
case 'Await': {
state.initialize(instrValue, state.kind(instrValue.value));
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
Effect.ConditionallyMutate,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
state.alias(lvalue, instrValue.value);
continuation = {kind: 'funeffects'};
break;
}
case 'TypeCastExpression': {
state.initialize(instrValue, state.kind(instrValue.value));
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
Effect.Read,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
state.alias(lvalue, instrValue.value);
continuation = {kind: 'funeffects'};
break;
}
case 'StartMemoize':
case 'FinishMemoize': {
for (const val of eachInstructionValueOperand(instrValue)) {
if (env.config.enablePreserveExistingMemoizationGuarantees) {
state.referenceAndRecordEffects(
freezeActions,
val,
Effect.Freeze,
ValueReason.Other,
);
} else {
state.referenceAndRecordEffects(
freezeActions,
val,
Effect.Read,
ValueReason.Other,
);
}
}
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
state.initialize(instrValue, {
kind: ValueKind.Frozen,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
state.define(lvalue, instrValue);
continuation = {kind: 'funeffects'};
break;
}
case 'LoadLocal': {
const lvalue = instr.lvalue;
const effect =
state.isDefined(lvalue) &&
state.kind(lvalue).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
state.referenceAndRecordEffects(
freezeActions,
instrValue.place,
effect,
ValueReason.Other,
);
lvalue.effect = Effect.ConditionallyMutate;
state.alias(lvalue, instrValue.place);
continuation = {kind: 'funeffects'};
break;
}
case 'LoadContext': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.place,
Effect.Capture,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.ConditionallyMutate;
const valueKind = state.kind(instrValue.place);
state.initialize(instrValue, valueKind);
state.define(lvalue, instrValue);
continuation = {kind: 'funeffects'};
break;
}
case 'DeclareLocal': {
const value = UndefinedValue;
state.initialize(
value,
instrValue.lvalue.kind === InstructionKind.Catch
? {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
}
: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
);
state.define(instrValue.lvalue.place, value);
continuation = {kind: 'funeffects'};
break;
}
case 'DeclareContext': {
state.initialize(instrValue, {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
});
state.define(instrValue.lvalue.place, instrValue);
continuation = {kind: 'funeffects'};
break;
}
case 'PostfixUpdate':
case 'PrefixUpdate': {
const effect =
state.isDefined(instrValue.lvalue) &&
state.kind(instrValue.lvalue).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
effect,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
state.alias(instrValue.lvalue, instrValue.value);
instrValue.lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'StoreLocal': {
const effect =
state.isDefined(instrValue.lvalue.place) &&
state.kind(instrValue.lvalue.place).kind === ValueKind.Context
? Effect.ConditionallyMutate
: Effect.Capture;
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
effect,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
state.alias(instrValue.lvalue.place, instrValue.value);
instrValue.lvalue.place.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'StoreContext': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
Effect.ConditionallyMutate,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.lvalue.place,
Effect.Mutate,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'StoreGlobal': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
Effect.Capture,
ValueReason.Other,
);
const lvalue = instr.lvalue;
lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'Destructure': {
let effect: Effect = Effect.Capture;
for (const place of eachPatternOperand(instrValue.lvalue.pattern)) {
if (
state.isDefined(place) &&
state.kind(place).kind === ValueKind.Context
) {
effect = Effect.ConditionallyMutate;
break;
}
}
state.referenceAndRecordEffects(
freezeActions,
instrValue.value,
effect,
ValueReason.Other,
);
const lvalue = instr.lvalue;
state.alias(lvalue, instrValue.value);
lvalue.effect = Effect.Store;
for (const place of eachPatternOperand(instrValue.lvalue.pattern)) {
state.alias(place, instrValue.value);
place.effect = Effect.Store;
}
continuation = {kind: 'funeffects'};
break;
}
case 'GetIterator': {
const kind = state.kind(instrValue.collection).kind;
const isMutable =
kind === ValueKind.Mutable || kind === ValueKind.Context;
let effect;
let valueKind: AbstractValue;
if (!isMutable || isArrayType(instrValue.collection.identifier)) {
effect = {
kind: Effect.Read,
reason: ValueReason.Other,
};
valueKind = {
kind: ValueKind.Mutable,
reason: new Set([ValueReason.Other]),
context: new Set(),
};
} else {
effect = {
kind: Effect.Capture,
reason: ValueReason.Other,
};
valueKind = state.kind(instrValue.collection);
}
continuation = {
kind: 'initialize',
effect,
valueKind,
lvalueEffect: Effect.Store,
};
break;
}
case 'IteratorNext': {
state.referenceAndRecordEffects(
freezeActions,
instrValue.iterator,
Effect.ConditionallyMutate,
ValueReason.Other,
);
state.referenceAndRecordEffects(
freezeActions,
instrValue.collection,
Effect.Capture,
ValueReason.Other,
);
state.initialize(instrValue, state.kind(instrValue.collection));
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = Effect.Store;
continuation = {kind: 'funeffects'};
break;
}
case 'NextPropertyOf': {
continuation = {
kind: 'initialize',
effect: {kind: Effect.Read, reason: ValueReason.Other},
lvalueEffect: Effect.Store,
valueKind: {
kind: ValueKind.Primitive,
reason: new Set([ValueReason.Other]),
context: new Set(),
},
};
break;
}
default: {
assertExhaustive(instrValue, 'Unexpected instruction kind');
}
}
if (continuation.kind === 'initialize') {
for (const operand of eachInstructionOperand(instr)) {
CompilerError.invariant(continuation.effect != null, {
reason: `effectKind must be set for instruction value \`${instrValue.kind}\``,
description: null,
loc: instrValue.loc,
suggestions: null,
});
state.referenceAndRecordEffects(
freezeActions,
operand,
continuation.effect.kind,
continuation.effect.reason,
);
}
state.initialize(instrValue, continuation.valueKind);
state.define(instr.lvalue, instrValue);
instr.lvalue.effect = continuation.lvalueEffect ?? defaultLvalueEffect;
}
functionEffects.push(...inferInstructionFunctionEffects(env, state, instr));
freezeActions.forEach(({values, reason}) =>
state.freezeValues(values, reason),
);
}
const terminalFreezeActions: Array<FreezeAction> = [];
for (const operand of eachTerminalOperand(block.terminal)) {
let effect;
if (block.terminal.kind === 'return' || block.terminal.kind === 'throw') {
if (
state.isDefined(operand) &&
state.kind(operand).kind === ValueKind.Context
) {
effect = Effect.ConditionallyMutate;
} else {
effect = Effect.Freeze;
}
} else {
effect = Effect.Read;
}
state.referenceAndRecordEffects(
terminalFreezeActions,
operand,
effect,
ValueReason.Other,
);
}
functionEffects.push(...inferTerminalFunctionEffects(state, block));
terminalFreezeActions.forEach(({values, reason}) =>
state.freezeValues(values, reason),
);
}
function hasContextRefOperand(
state: InferenceState,
instrValue: InstructionValue,
): boolean {
for (const place of eachInstructionValueOperand(instrValue)) {
if (
state.isDefined(place) &&
state.kind(place).kind === ValueKind.Context
) {
return true;
}
}
return false;
}
export function getFunctionCallSignature(
env: Environment,
type: Type,
): FunctionSignature | null {
if (type.kind !== 'Function') {
return null;
}
return env.getFunctionSignature(type);
}
export function getFunctionEffects(
fn: MethodCall | CallExpression,
sig: FunctionSignature,
): Array<Effect> | null {
const results = [];
for (let i = 0; i < fn.args.length; i++) {
const arg = fn.args[i];
if (i < sig.positionalParams.length) {
if (arg.kind === 'Identifier') {
results.push(sig.positionalParams[i]);
} else {
return null;
}
} else if (sig.restParam !== null) {
results.push(sig.restParam);
} else {
return null;
}
}
return results;
}
function areArgumentsImmutableAndNonMutating(
state: InferenceState,
args: MethodCall['args'],
): boolean {
for (const arg of args) {
const place = arg.kind === 'Identifier' ? arg : arg.place;
const kind = state.kind(place).kind;
switch (kind) {
case ValueKind.Global:
case ValueKind.Primitive:
case ValueKind.Frozen: {
break;
}
default: {
return false;
}
}
const values = state.values(place);
for (const value of values) {
if (
value.kind === 'FunctionExpression' &&
value.loweredFunc.func.params.some(param => {
const place = param.kind === 'Identifier' ? param : param.place;
const range = place.identifier.mutableRange;
return range.end > range.start + 1;
})
) {
return false;
}
}
}
return true;
}