more item cleanup

This commit is contained in:
nora 2023-08-03 13:50:14 +02:00
parent b021a9e218
commit 73a369730b
12 changed files with 104 additions and 88 deletions

View file

@ -98,12 +98,21 @@ export class ItemId {
}
export type ItemKind<P extends Phase> =
| ItemFunction<P>
| ItemType<P>
| ItemImport<P>
| ItemMod<P>
| ItemExtern
| ItemGlobal<P>;
| ItemKindFunction<P>
| ItemKindType<P>
| ItemKindImport<P>
| ItemKindMod<P>
| ItemKindExtern
| ItemKindGlobal<P>;
type ItemVariant<Variant, P extends Phase> = Variant & Item<P>;
export type ItemFunction<P extends Phase> = ItemVariant<ItemKindFunction<P>, P>;
export type ItemType<P extends Phase> = ItemVariant<ItemKindType<P>, P>;
export type ItemImport<P extends Phase> = ItemVariant<ItemKindImport<P>, P>;
export type ItemMod<P extends Phase> = ItemVariant<ItemKindMod<P>, P>;
export type ItemExtern<P extends Phase> = ItemVariant<ItemKindExtern, P>;
export type ItemGlobal<P extends Phase> = ItemVariant<ItemKindGlobal<P>, P>;
export type Item<P extends Phase> = ItemKind<P> & {
span: Span;
@ -111,7 +120,7 @@ export type Item<P extends Phase> = ItemKind<P> & {
name: string;
} & P["defPath"];
export type ItemFunction<P extends Phase> = {
export type ItemKindFunction<P extends Phase> = {
kind: "function";
params: FunctionArg<P>[];
body: Expr<P>;
@ -125,7 +134,7 @@ export type FunctionArg<P extends Phase> = {
span: Span;
};
export type ItemType<P extends Phase> = {
export type ItemKindType<P extends Phase> = {
kind: "type";
type: TypeDefKind<P>;
ty?: TyStruct;
@ -146,7 +155,7 @@ export type FieldDef<P extends Phase> = {
type: Type<P>;
};
export type ItemImport<P extends Phase> = {
export type ItemKindImport<P extends Phase> = {
kind: "import";
module: StringLiteral;
func: StringLiteral;
@ -155,14 +164,14 @@ export type ItemImport<P extends Phase> = {
ty?: TyFn;
};
export type ItemMod<P extends Phase> = {
export type ItemKindMod<P extends Phase> = {
kind: "mod";
contents: Item<P>[];
};
export type ItemExtern = { kind: "extern" };
export type ItemKindExtern = { kind: "extern" };
export type ItemGlobal<P extends Phase> = {
export type ItemKindGlobal<P extends Phase> = {
kind: "global";
type: Type<P>;
init: Expr<P>;

View file

@ -258,7 +258,7 @@ export function lower(gcx: GlobalContext): wasm.Module {
return mod;
}
function lowerImport(cx: Context, def: ItemImport<Typecked> & Item<Typecked>) {
function lowerImport(cx: Context, def: ItemImport<Typecked>) {
const existing = cx.mod.imports.findIndex(
(imp) => imp.module === def.module.value && imp.name === def.func.value,
);
@ -285,7 +285,7 @@ function lowerImport(cx: Context, def: ItemImport<Typecked> & Item<Typecked>) {
cx.funcIndices.set({ kind: "item", id: def.id }, { kind: "import", idx });
}
function lowerGlobal(cx: Context, def: ItemGlobal<Typecked> & Item<Typecked>) {
function lowerGlobal(cx: Context, def: ItemGlobal<Typecked>) {
const globalIdx = cx.mod.globals.length;
let valtype: "i32" | "i64";
@ -320,7 +320,7 @@ function lowerGlobal(cx: Context, def: ItemGlobal<Typecked> & Item<Typecked>) {
type FuncContext = {
cx: Context;
func: Item<Typecked> & ItemFunction<Typecked>;
func: ItemFunction<Typecked>;
wasmType: wasm.FuncType;
wasm: wasm.Func;
varLocations: VarLocation[];
@ -346,7 +346,7 @@ type StructLayout = {
fields: StructFieldLayout[];
};
function lowerFunc(cx: Context, func: Item<Typecked> & ItemFunction<Typecked>) {
function lowerFunc(cx: Context, func: ItemFunction<Typecked>) {
const abi = computeAbi(func.ty!);
const { type: wasmType, paramLocations } = wasmTypeForAbi(abi, func.ty!);
const type = internFuncType(cx, wasmType);

View file

@ -234,7 +234,7 @@ function parseItem(t: State): [State, Item<Parsed>] {
[t, init] = parseExpr(t);
[t] = expectNext(t, ";");
const global: ItemGlobal<Parsed> & Item<Parsed> = {
const global: ItemGlobal<Parsed> = {
kind: "global",
name: name.ident,
type,

View file

@ -54,7 +54,7 @@ function printItem(item: Item<AnyPhase>): string {
}
}
function printFunction(func: ItemFunction<AnyPhase> & Item<AnyPhase>): string {
function printFunction(func: ItemFunction<AnyPhase>): string {
const args = func.params
.map(({ name, type }) => `${name}: ${printType(type)}`)
.join(", ");
@ -62,7 +62,7 @@ function printFunction(func: ItemFunction<AnyPhase> & Item<AnyPhase>): string {
return `function ${func.name}(${args})${ret} = ${printExpr(func.body, 0)};`;
}
function printTypeDef(type: ItemType<AnyPhase> & Item<AnyPhase>): string {
function printTypeDef(type: ItemType<AnyPhase>): string {
switch (type.type.kind) {
case "struct": {
const { fields } = type.type;
@ -92,7 +92,7 @@ function printImportDef(def: ItemImport<AnyPhase>): string {
)}(${args})${ret};`;
}
function printMod(mod: ItemMod<AnyPhase> & Item<AnyPhase>): string {
function printMod(mod: ItemMod<AnyPhase>): string {
return `mod ${mod.name} (\n${mod.contents.map(printItem).join("\n ")});`;
}

View file

@ -43,7 +43,7 @@ function loadCrate(cx: Context, name: string, span: Span): Map<string, ItemId> {
function resolveModItem(
cx: Context,
mod: (ItemMod<Built> | ItemExtern) & Item<Built>,
mod: ItemMod<Built> | ItemExtern<Built>,
name: string,
): ItemId | undefined {
const cachedContents = cx.modContentsCache.get(mod.id);