mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
refactor crate loading
This commit is contained in:
parent
dd93453943
commit
beb0321382
9 changed files with 131 additions and 99 deletions
49
src/context.ts
Normal file
49
src/context.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { Crate, DepCrate, Final, Item, ItemId, Phase } from "./ast";
|
||||
import { DUMMY_SPAN, Span } from "./error";
|
||||
import { Ids, unwrap } from "./utils";
|
||||
|
||||
export type CrateLoader = (
|
||||
gcx: GlobalContext,
|
||||
name: string,
|
||||
span: Span
|
||||
) => DepCrate;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* There is a "downstream"/"binary"/"final" crate with crateId=0, where `function main()` lives, but
|
||||
* dependencies (which also use the same context) do not care about that.
|
||||
*/
|
||||
export class GlobalContext {
|
||||
public depCrates: Crate<Final>[] = [];
|
||||
public crateId: Ids = new Ids();
|
||||
|
||||
constructor(public crateLoader: CrateLoader) {}
|
||||
|
||||
public findItem<P extends Phase>(
|
||||
id: ItemId,
|
||||
localCrate: Crate<P>
|
||||
): Item<P | Final> {
|
||||
const crate = unwrap(
|
||||
[localCrate, ...this.depCrates].find((crate) => crate.id === id.crateId)
|
||||
);
|
||||
|
||||
if (id.itemIdx === 0) {
|
||||
// Return a synthetic module representing the crate root.
|
||||
return {
|
||||
kind: "mod",
|
||||
node: {
|
||||
contents: crate.rootItems,
|
||||
name: crate.packageName,
|
||||
},
|
||||
span: DUMMY_SPAN,
|
||||
id,
|
||||
};
|
||||
}
|
||||
|
||||
return unwrap(crate.itemsById.get(id));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue