Skip to content

Commit 3c34fde

Browse files
committed
[compiler] More readable alias signature declarations
Now that we have support for defining aliasing signatures in moduleTypeProvider, which uses string names for receiver/args/returns/etc, we can reuse that same form for builtin declarations. The declarations are written in the unparsed form and than parsed/validated when registered (in the addFunction/addHook call). This also required flushing out configs/schemas for more effect types.
1 parent fd3a801 commit 3c34fde

File tree

4 files changed

+327
-166
lines changed

4 files changed

+327
-166
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts

Lines changed: 13 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {
9-
Effect,
10-
GeneratedSource,
11-
makeIdentifierId,
12-
Place,
13-
ValueKind,
14-
ValueReason,
15-
} from './HIR';
8+
import {Effect, ValueKind, ValueReason} from './HIR';
169
import {
1710
BUILTIN_SHAPES,
1811
BuiltInArrayId,
@@ -41,18 +34,12 @@ import {
4134
addFunction,
4235
addHook,
4336
addObject,
44-
signatureArgument,
4537
} from './ObjectShape';
4638
import {BuiltInType, ObjectType, PolyType} from './Types';
47-
import {
48-
AliasingEffectConfig,
49-
AliasingSignatureConfig,
50-
TypeConfig,
51-
} from './TypeSchema';
39+
import {TypeConfig} from './TypeSchema';
5240
import {assertExhaustive} from '../Utils/utils';
5341
import {isHookName} from './Environment';
5442
import {CompilerError, SourceLocation} from '..';
55-
import {AliasingEffect, AliasingSignature} from '../Inference/AliasingEffects';
5643

5744
/*
5845
* This file exports types and defaults for JavaScript global objects.
@@ -658,35 +645,35 @@ const REACT_APIS: Array<[string, BuiltInType]> = [
658645
hookKind: 'useEffect',
659646
returnValueKind: ValueKind.Frozen,
660647
aliasing: {
661-
receiver: makeIdentifierId(0),
648+
receiver: '@receiver',
662649
params: [],
663-
rest: makeIdentifierId(1),
664-
returns: makeIdentifierId(2),
665-
temporaries: [signatureArgument(3)],
650+
rest: '@rest',
651+
returns: '@returns',
652+
temporaries: ['@effect'],
666653
effects: [
667654
// Freezes the function and deps
668655
{
669656
kind: 'Freeze',
670-
value: signatureArgument(1),
657+
value: '@rest',
671658
reason: ValueReason.Effect,
672659
},
673660
// Internally creates an effect object that captures the function and deps
674661
{
675662
kind: 'Create',
676-
into: signatureArgument(3),
663+
into: '@effect',
677664
value: ValueKind.Frozen,
678665
reason: ValueReason.KnownReturnSignature,
679666
},
680667
// The effect stores the function and dependencies
681668
{
682669
kind: 'Capture',
683-
from: signatureArgument(1),
684-
into: signatureArgument(3),
670+
from: '@rest',
671+
into: '@effect',
685672
},
686673
// Returns undefined
687674
{
688675
kind: 'Create',
689-
into: signatureArgument(2),
676+
into: '@returns',
690677
value: ValueKind.Primitive,
691678
reason: ValueReason.KnownReturnSignature,
692679
},
@@ -903,10 +890,6 @@ export function installTypeConfig(
903890
}
904891
}
905892
case 'function': {
906-
const aliasing =
907-
typeConfig.aliasing != null
908-
? parseAliasingSignatureConfig(typeConfig.aliasing, moduleName, loc)
909-
: null;
910893
return addFunction(shapes, [], {
911894
positionalParams: typeConfig.positionalParams,
912895
restParam: typeConfig.restParam,
@@ -922,14 +905,10 @@ export function installTypeConfig(
922905
noAlias: typeConfig.noAlias === true,
923906
mutableOnlyIfOperandsAreMutable:
924907
typeConfig.mutableOnlyIfOperandsAreMutable === true,
925-
aliasing,
908+
aliasing: typeConfig.aliasing,
926909
});
927910
}
928911
case 'hook': {
929-
const aliasing =
930-
typeConfig.aliasing != null
931-
? parseAliasingSignatureConfig(typeConfig.aliasing, moduleName, loc)
932-
: null;
933912
return addHook(shapes, {
934913
hookKind: 'Custom',
935914
positionalParams: typeConfig.positionalParams ?? [],
@@ -944,7 +923,7 @@ export function installTypeConfig(
944923
),
945924
returnValueKind: typeConfig.returnValueKind ?? ValueKind.Frozen,
946925
noAlias: typeConfig.noAlias === true,
947-
aliasing,
926+
aliasing: typeConfig.aliasing,
948927
});
949928
}
950929
case 'object': {
@@ -987,90 +966,6 @@ export function installTypeConfig(
987966
}
988967
}
989968

990-
function parseAliasingSignatureConfig(
991-
typeConfig: AliasingSignatureConfig,
992-
moduleName: string,
993-
loc: SourceLocation,
994-
): AliasingSignature {
995-
const lifetimes = new Map<string, Place>();
996-
function define(temp: string): Place {
997-
CompilerError.invariant(!lifetimes.has(temp), {
998-
reason: `Invalid type configuration for module`,
999-
description: `Expected aliasing signature to have unique names for receiver, params, rest, returns, and temporaries in module '${moduleName}'`,
1000-
loc,
1001-
});
1002-
const place = signatureArgument(lifetimes.size);
1003-
lifetimes.set(temp, place);
1004-
return place;
1005-
}
1006-
function lookup(temp: string): Place {
1007-
const place = lifetimes.get(temp);
1008-
CompilerError.invariant(place != null, {
1009-
reason: `Invalid type configuration for module`,
1010-
description: `Expected aliasing signature effects to reference known names from receiver/params/rest/returns/temporaries, but '${temp}' is not a known name in '${moduleName}'`,
1011-
loc,
1012-
});
1013-
return place;
1014-
}
1015-
const receiver = define(typeConfig.receiver);
1016-
const params = typeConfig.params.map(define);
1017-
const rest = typeConfig.rest != null ? define(typeConfig.rest) : null;
1018-
const returns = define(typeConfig.returns);
1019-
const temporaries = typeConfig.temporaries.map(define);
1020-
const effects = typeConfig.effects.map(
1021-
(effect: AliasingEffectConfig): AliasingEffect => {
1022-
switch (effect.kind) {
1023-
case 'Assign': {
1024-
return {
1025-
kind: 'Assign',
1026-
from: lookup(effect.from),
1027-
into: lookup(effect.into),
1028-
};
1029-
}
1030-
case 'Create': {
1031-
return {
1032-
kind: 'Create',
1033-
into: lookup(effect.into),
1034-
reason: ValueReason.KnownReturnSignature,
1035-
value: effect.value,
1036-
};
1037-
}
1038-
case 'Freeze': {
1039-
return {
1040-
kind: 'Freeze',
1041-
value: lookup(effect.value),
1042-
reason: ValueReason.KnownReturnSignature,
1043-
};
1044-
}
1045-
case 'Impure': {
1046-
return {
1047-
kind: 'Impure',
1048-
place: lookup(effect.place),
1049-
error: CompilerError.throwTodo({
1050-
reason: 'Support impure effect declarations',
1051-
loc: GeneratedSource,
1052-
}),
1053-
};
1054-
}
1055-
default: {
1056-
assertExhaustive(
1057-
effect,
1058-
`Unexpected effect kind '${(effect as any).kind}'`,
1059-
);
1060-
}
1061-
}
1062-
},
1063-
);
1064-
return {
1065-
receiver: receiver.identifier.id,
1066-
params: params.map(p => p.identifier.id),
1067-
rest: rest != null ? rest.identifier.id : null,
1068-
returns: returns.identifier.id,
1069-
temporaries,
1070-
effects,
1071-
};
1072-
}
1073-
1074969
export function getReanimatedModuleType(registry: ShapeRegistry): ObjectType {
1075970
// hooks that freeze args and return frozen value
1076971
const frozenHooks = [

compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,20 @@ export const ValueKindSchema = z.enum([
14531453
ValueKind.Context,
14541454
]);
14551455

1456+
export const ValueReasonSchema = z.enum([
1457+
ValueReason.Context,
1458+
ValueReason.Effect,
1459+
ValueReason.Global,
1460+
ValueReason.HookCaptured,
1461+
ValueReason.HookReturn,
1462+
ValueReason.JsxCaptured,
1463+
ValueReason.KnownReturnSignature,
1464+
ValueReason.Other,
1465+
ValueReason.ReactiveFunctionArgument,
1466+
ValueReason.ReducerState,
1467+
ValueReason.State,
1468+
]);
1469+
14561470
// The effect with which a value is modified.
14571471
export enum Effect {
14581472
// Default value: not allowed after lifetime inference

0 commit comments

Comments
 (0)