/** * The checker's working state: scopes, bindings, and the answers as they are * worked out. * * Everything mutable in the check lives here and nowhere else, so the passes * over the tree are ordinary functions that take this and a node. The state is * handed out as a `CheckedScript` at the end and is not touched again. * * Nothing in this file decides a rule of the language. It holds what the rules * are decided from: which names are in scope here, what the source said, and * where to put an answer once one of the passes has it. */ import type { Call, Expression, FunctionDeclaration, Name, NameReference, Script } from '../ast/index.js'; import type { DiagnosticCode, DiagnosticValues } from '../catalogue/index.js'; import type { DiagnosticSink } from '../diagnostics/index.js'; import type { SourceFile } from '../source/index.js'; import type { Span } from '../span/index.js'; import type { Binding, BindingKind, CheckedCall, CheckedDeclaration, CheckedFunction, CheckedInput, CheckedOutput, CheckedRequest, CheckedScript, Mutable } from './checked.js'; import type { Type } from './types.js'; import type { Warmup } from './warmup.js'; /** * One nesting of names, `language.md` 12.1. * * The global scope is not one of these: the library is a lookup of its own, * consulted after every scope has been tried, because it holds three hundred * names that never change and copying them into a map per compile would be the * most expensive thing the checker does. */ export interface Scope { readonly kind: 'file' | 'block' | 'function'; readonly names: Map>; readonly parent: Scope | undefined; /** The loop variable this scope introduced, which OS2006 refuses assignment to. */ readonly loopVariable: string | undefined; } /** * What surrounds the statement being checked, for OS3006. * * Whether the thing being checked runs on every bar is deliberately not here. * It is a fact about a node rather than about the path the pass took to reach * it, and half of what makes a call conditional is an operator rather than a * statement, so `conditional.ts` answers it from the tree and this carries only * what a pass can know. */ export interface Placement { /** True only for a statement written directly in the file, `language.md` 15.3. */ readonly topLevel: boolean; /** The enclosing construct, named in OS3006's message. */ readonly construct: string | undefined; /** Inside a `for` or `while` body, where `break` and `continue` are legal. */ readonly inLoop: boolean; } export declare const TOP_LEVEL: Placement; export declare class Checker { readonly file: SourceFile; readonly sink: DiagnosticSink; readonly script: Script; /** The file scope, which is also where a `fn` puts its name. */ readonly fileScope: Scope; scope: Scope; declaration: CheckedDeclaration | undefined; readonly inputs: Mutable[]; readonly outputs: CheckedOutput[]; readonly functions: Mutable[]; readonly bindings: Mutable[]; readonly calls: CheckedCall[]; readonly requests: CheckedRequest[]; readonly types: Map; readonly warmups: Map; /** * The expressions a declaration handle is allowed to stand in, 5.4. * * `handles.ts` holds the rule and fills this. It is a set of permissions * rather than a flag on the pass, because the permission belongs to one * written expression: `fill(upper, lower)` may name a handle, and an * expression inside one of those arguments may not. */ readonly handleSites: Set; readonly references: Map; readonly targets: Map; readonly callSites: Map; /** * The multi-output call a name holds, by binding, for `m[1]`. * * A warmup is recorded against an expression, and an element's warmup is not * a fact about the array expression: it is a fact about the call the name was * given, which `m[1]` has to reach two statements later. Only the calls whose * outputs warm up at different bars are kept, because for every other one the * array's warmup is already the element's. */ private readonly multiOutputs; /** The declarations of every `fn`, collected before any body is checked. */ readonly functionsByName: Map; readonly functionNodes: Map; /** Bodies already checked, and the ones being checked, so a cycle stops. */ readonly checkedFunctions: Set; readonly checkingFunctions: Set; repaints: boolean; stateCount: number; /** How deep inside a `req` expression the pass is, `stdlib.md` 15.4. */ requestDepth: number; /** The function whose body is being checked, for its own statefulness. */ currentFunction: Mutable | undefined; /** What the body being checked returns, gathered as the `return`s are met. */ pendingReturns: { readonly type: Type; readonly warmup: Warmup; }[]; /** Every call site a bar can pass without evaluating, `language.md` 11.4. */ private readonly conditional; /** * The names some line gives a value, read before a `var` is called never. * * Built the first time it is asked for, because it is one walk of the whole * tree and almost every file never reads a `var` that is still never. */ private givenAValue; constructor(file: SourceFile, script: Script, sink: DiagnosticSink); /** * Whether this call site can be skipped on a bar, which is what OS8001 asks. * * Asked of the call rather than of the pass that reached it, so the answer is * the same wherever the call was written: inside an `if`, in a ternary arm, * or on the right of an `and` that short-circuited. */ isConditional(call: Call): boolean; report(code: Code, span: Span, values: DiagnosticValues[Code]): void; /** The source text a span covers, for the codes that quote what was written. */ textOf(span: Span): string; /** Opens a scope, runs the body in it, and closes it however the body ends. */ inScope(kind: Scope['kind'], loopVariable: string | undefined, body: () => T): T; /** * The declaration a name means here, and whether a function body stands * between the two. * * A plain assignment updates an enclosing name (`language.md` 12.2), and a * function body is the one place that does not hold: 12.3's own example makes * an assignment to a file-scope name from inside a `fn` OS2002. */ lookupAcross(name: string): { readonly binding: Mutable | undefined; readonly crossedFunction: boolean; }; /** The declaration a name means here, or nothing when no scope holds it. */ lookup(name: string): Mutable | undefined; /** Whether an enclosing scope already holds the name, which is OS2002. */ enclosing(name: string): Mutable | undefined; /** Whether the name belongs to the loop being executed, `language.md` 10.3. */ loopVariableNamed(name: string): boolean; /** Declares a name in the current scope and records it in the checked tree. */ declare(name: Name, kind: BindingKind, type: Type, warmup: Warmup, persistence?: Binding['persistence']): Mutable; /** * Remembers, or forgets, the multi-output call a name was just given. * * Forgetting on a reassignment rather than keeping the first call is what * stops `m[1]` from being answered about a call the name no longer holds: * the array's own warmup is the fallback, and it is a floor for every * element. */ holdsMultiOutput(binding: number, call: CheckedCall | undefined): void; /** The multi-output call this name holds here, when it holds one. */ multiOutputOf(name: string): CheckedCall | undefined; record(expression: Expression, type: Type, warmup: Warmup): Type; typeOf(expression: Expression): Type; /** * The warmup a read of this name sees, here. * * A `var` that is still never at this line but that a later line gives a * value is not never: the later line wrote it on the bar before, or on an * earlier pass of the same loop. The honest answer is a floor of bar 0, * which claims nothing and withdraws the claim OS8009 is built on, where an * exact bar would need the later line's warmup before it has been read. */ warmupOfRead(binding: Binding): Warmup; warmupOf(expression: Expression): Warmup; /** Every name a reader could have meant here: the scopes, then the library. */ namesInScope(): readonly string[]; suggestionFor(written: string): string; /** * OS2010's suggestion: the closest function, called with what was written. * * The catalogue documents the slot as the library function whose name is * closest, with its first argument filled in, so `volume(20)` is offered a * function over `volume` and the arguments the reader already wrote, * rather than a name that cannot be called either. */ callSuggestionFor(written: string, args: readonly string[]): string; /** A state region for one call site, `compiled-program.md` 2.11. */ takeStateId(): number; finish(): CheckedScript; } /** * OS7001: a name the library gives only to a file declared with `strategy()`. * * The message names the line of the `study()` declaration rather than the call, * because that is the line the reader has to change: the call is what they * meant, and the declaration is what refuses it. */ export declare function reportStrategyOnly(checker: Checker, name: string, span: Span, strategyOnly: boolean): void; /** * Promotes the top-level names whose history is read to series registers. * * `compiled-program.md` 2.10 allocates a register only for a name the program * reads the past of, and this is the one place that decides it, once every read * in the file has been seen. */ export declare function finaliseStorage(checker: Checker): void; //# sourceMappingURL=checker.d.ts.map