Skip to content

AST

AST represents the parsed or checked expression graph plus metadata.

new AST(exprValue, sourceInfoValue?, typeMap?, refMap?, sourceValue?): AST

Expr

SourceInfo

Map<number, Type> = ...

Map<number, ReferenceInfo> = ...

Source

AST

clearUnusedIds(): void

ClearUnusedIds removes source offsets that do not correspond to live ids.

void


expr(): Expr

Expr returns the root expression.

Expr


getOverloadIds(id): string[]

GetOverloadIds returns the overload ids for an expression id.

number

string[]


getType(id): Type | undefined

GetType looks up the type for an expression id.

number

Type | undefined


ids(): Set<number>

Ids returns the set of ids referenced by the AST and macro calls.

Set<number>


isChecked(): boolean

IsChecked reports whether type-checking metadata is present.

boolean


referenceMap(): Map<number, ReferenceInfo>

ReferenceMap returns a copy of the expression id to reference map.

Map<number, ReferenceInfo>


referenceMapView(): ReadonlyMap<number, ReferenceInfo>

ReferenceMapView returns the expression id to reference map without copying it.

Copying the map is the dominant cost of planning a small expression once the runtime dispatcher is shared. Readers that never mutate the result and do not outlive the AST use this accessor; every other caller uses referenceMap().

ReadonlyMap<number, ReferenceInfo>


setReference(id, reference): void

SetReference records the reference for an expression id.

number

ReferenceInfo

void


setType(id, type): void

SetType records the type for an expression id.

number

Type

void


source(): Source

Source returns the original source when available, or a metadata-backed source otherwise.

Source


sourceInfo(): SourceInfo

SourceInfo returns the associated source metadata.

SourceInfo


toCheckedExpr(): CheckedExpr

toCheckedExpr converts a checked AST to a CheckedExpr protobuf value.

CheckedExpr


toParsedExpr(): ParsedExpr

toParsedExpr converts an unchecked AST to a ParsedExpr protobuf value.

ParsedExpr


toProto(): ParsedExpr | CheckedExpr

toProto converts the AST to either ParsedExpr or CheckedExpr depending on whether it is checked.

ParsedExpr | CheckedExpr


typeMap(): Map<number, Type>

TypeMap returns a copy of the expression id to type map.

Map<number, Type>


withSource(source): AST

WithSource returns an equivalent AST bound to a source, sharing this AST’s checked metadata.

Rebuilding an AST through typeMap() and referenceMap() deep-copies both maps, and copying the type map structured-clones every entry. Callers that attach a source to an AST they just produced and are about to discard pay that copy for nothing: check spent roughly a third of its total time there. Sharing is safe only because the receiver is a temporary; use the copying accessors when the original AST outlives the result.

Source

AST