diff --git a/src/typeck/base.ts b/src/typeck/base.ts new file mode 100644 index 0000000..eb42073 --- /dev/null +++ b/src/typeck/base.ts @@ -0,0 +1,35 @@ +import { ItemId, Pkg, Resolved, Ty } from "../ast"; +import { GlobalContext } from "../context"; +import { CompilerError, ErrorEmitted } from "../error"; +import { ComplexMap } from "../utils"; + +export type TypeckCtx = { + gcx: GlobalContext; + /** + * A cache of all item types. + * Starts off as undefined, then gets set to null + * while computing the type (for cycle detection) and + * afterwards, we get the ty. + */ + itemTys: ComplexMap; + ast: Pkg; +}; + +export function mkTyFn(params: Ty[], returnTy: Ty): Ty { + return { kind: "fn", params, returnTy }; +} + +export function tyError(cx: TypeckCtx, err: CompilerError): Ty { + return { + kind: "error", + err: emitError(cx, err), + }; +} + +export function tyErrorFrom(prev: { err: ErrorEmitted }): Ty { + return { kind: "error", err: prev.err }; +} + +export function emitError(cx: TypeckCtx, err: CompilerError): ErrorEmitted { + return cx.gcx.error.emit(err); +} diff --git a/src/typeck/expr.ts b/src/typeck/expr.ts index ab2f95c..34ab9c3 100644 --- a/src/typeck/expr.ts +++ b/src/typeck/expr.ts @@ -28,14 +28,10 @@ import { } from "../ast"; import { CompilerError, ErrorEmitted, Span, unreachable } from "../error"; import { printTy } from "../printer"; +import { TypeckCtx, emitError, mkTyFn, tyError, tyErrorFrom } from "./base"; import { InferContext } from "./infer"; import { - TypeckCtx, - emitError, lowerAstTy, - mkTyFn, - tyError, - tyErrorFrom, typeOfItem, } from "./item"; diff --git a/src/typeck/index.ts b/src/typeck/index.ts index 1c221bf..0b996f3 100644 --- a/src/typeck/index.ts +++ b/src/typeck/index.ts @@ -17,9 +17,10 @@ import { import { GlobalContext } from "../context"; import { CompilerError, ErrorEmitted, Span } from "../error"; import { ComplexMap } from "../utils"; +import { emitError } from "./base"; import { checkBody, exprError } from "./expr"; import { InferContext } from "./infer"; -import { emitError, typeOfItem } from "./item"; +import { typeOfItem } from "./item"; export function typeck( gcx: GlobalContext, diff --git a/src/typeck/item.ts b/src/typeck/item.ts index 3f996d4..bb5cd15 100644 --- a/src/typeck/item.ts +++ b/src/typeck/item.ts @@ -1,5 +1,4 @@ import { - Pkg, ItemId, Resolved, Ty, @@ -12,41 +11,9 @@ import { Type, substituteTy, } from "../ast"; -import { GlobalContext } from "../context"; -import { CompilerError, ErrorEmitted, Span } from "../error"; +import { CompilerError, Span } from "../error"; import { printTy } from "../printer"; -import { ComplexMap } from "../utils"; - -export type TypeckCtx = { - gcx: GlobalContext; - /** - * A cache of all item types. - * Starts off as undefined, then gets set to null - * while computing the type (for cycle detection) and - * afterwards, we get the ty. - */ - itemTys: ComplexMap; - ast: Pkg; -}; - -export function mkTyFn(params: Ty[], returnTy: Ty): Ty { - return { kind: "fn", params, returnTy }; -} - -export function tyError(cx: TypeckCtx, err: CompilerError): Ty { - return { - kind: "error", - err: emitError(cx, err), - }; -} - -export function tyErrorFrom(prev: { err: ErrorEmitted }): Ty { - return { kind: "error", err: prev.err }; -} - -export function emitError(cx: TypeckCtx, err: CompilerError): ErrorEmitted { - return cx.gcx.error.emit(err); -} +import { TypeckCtx, tyError, tyErrorFrom } from "./base"; function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty { switch (name) { @@ -68,6 +35,10 @@ function builtinAsTy(cx: TypeckCtx, name: string, span: Span): Ty { } } +/** + * Lowers the AST representation of a type into its resolved Ty representation. + * Will also validate the type, for example ensuring that generic arguments match up. + */ // TODO: Cleanup, maybe get the ident switch into this function because typeOfItem is unused. export function lowerAstTy(cx: TypeckCtx, type: Type): Ty { switch (type.kind) {