rename crate to pkg

This commit is contained in:
nora 2023-12-15 18:35:20 +01:00
parent 7ca78530a1
commit b273b20a75
11 changed files with 124 additions and 124 deletions

View file

@ -1,76 +1,76 @@
import { Crate, DepCrate, Final, Item, ItemId, Phase } from "./ast";
import { Pkg, DepPkg, Final, Item, ItemId, Phase } from "./ast";
import { ErrorHandler, Span } from "./error";
import { Ids, unwrap } from "./utils";
import fs from "fs";
import path from "path";
export type CrateLoader = (
export type PkgLoader = (
gcx: GlobalContext,
name: string,
span: Span,
) => DepCrate;
) => DepPkg;
/**
* The global context containing information about the _global compilation session_,
* like loaded crates.
* Notably, the global context is _not_ supposed to have information specific to the "local crate",
* because with the current compilation model, there is no "local crate" in a session.
* like loaded pkgs.
* Notably, the global context is _not_ supposed to have information specific to the "local pkg",
* because with the current compilation model, there is no "local pkg" in a session.
*
* There is a "downstream"/"binary"/"final" crate with crateId=0, where `function main()` lives, but
* There is a "downstream"/"binary"/"final" pkg with pkgId=0, where `function main()` lives, but
* dependencies (which also use the same context) do not care about that.
*/
export class GlobalContext {
public error: ErrorHandler = new ErrorHandler();
public finalizedCrates: Crate<Final>[] = [];
public finalizedPkgs: Pkg<Final>[] = [];
// For cycle detection.
public cratesBeingLoaded: Set<string> = new Set<string>();
public crateId: Ids = new Ids();
public pkgsBeingLoaded: Set<string> = new Set<string>();
public pkgId: Ids = new Ids();
constructor(public opts: Options, public crateLoader: CrateLoader) {}
constructor(public opts: Options, public pkgLoader: PkgLoader) {}
public findItem<P extends Phase>(
id: ItemId,
localCrate?: Crate<P>,
localPkg?: Pkg<P>,
): Item<P> | Item<Final> {
const allCrates: (Crate<P> | Crate<Final>)[] = [
...(localCrate ? [localCrate] : []),
...this.finalizedCrates,
const allPkgs: (Pkg<P> | Pkg<Final>)[] = [
...(localPkg ? [localPkg] : []),
...this.finalizedPkgs,
];
const crate: Crate<P> | Crate<Final> = unwrap(
allCrates.find((crate) => crate.id === id.crateId),
const pkg: Pkg<P> | Pkg<Final> = unwrap(
allPkgs.find((pkg) => pkg.id === id.pkgId),
);
if (crate.fatalError) {
if (pkg.fatalError) {
return {
kind: "error",
defPath: [],
err: crate.fatalError,
id: new ItemId(crate.id, 0),
err: pkg.fatalError,
id: new ItemId(pkg.id, 0),
name: "",
span: Span.startOfFile(crate.rootFile),
span: Span.startOfFile(pkg.rootFile),
};
}
if (id.itemIdx === 0) {
const contents: Item<P>[] | Item<Final>[] = crate.rootItems;
const contents: Item<P>[] | Item<Final>[] = pkg.rootItems;
// Typescript does not seem to be able to understand this here.
// The type of this is supposed to be (Item<P> | Item<Final>)["contents"] which is
// "too complex to represent".
const erasedContents: any = contents;
// Return a synthetic module representing the crate root.
// Return a synthetic module representing the pkg root.
const mod: Item<P> | Item<Final> = {
kind: "mod",
name: crate.packageName,
name: pkg.packageName,
contents: erasedContents,
span: Span.startOfFile(crate.rootFile),
span: Span.startOfFile(pkg.rootFile),
id,
};
return mod;
}
const mod = unwrap(crate.itemsById.get(id));
const mod = unwrap(pkg.itemsById.get(id));
return mod;
}
}