/** * Expression IR — Type-directed lowering for the emitter. * * This module implements the "lowering" pass of the transpiler: * given an untyped data literal (from @factory sets or @coerce expansion) * and a target type (TypeNode/PropertyNode), produce a typed Expr tree. * * The Expr tree is language-agnostic. Per-language visitors in render-expr.ts * walk it to produce target language code. * * Architecture (following TypeScript/Roslyn/Babel pattern): * Data literal + Type graph → resolve() → Expr tree → visit() → code string */ import { TypeNode, TypeName } from "./ast.js"; /** A literal string value. */ export interface StringLiteral { kind: "string"; value: string; } /** A literal number value. */ export interface NumberLiteral { kind: "number"; value: number; } /** A literal boolean value. */ export interface BooleanLiteral { kind: "boolean"; value: boolean; } /** A null/None/nil value. */ export interface NullLiteral { kind: "null"; } /** Reference to a factory/coercion parameter (substituted at runtime). */ export interface ParamRef { kind: "param"; name: string; paramType: string; } /** Direct construction of a non-polymorphic type. */ export interface Construct { kind: "construct"; typeName: TypeName; fields: FieldAssignment[]; } /** * Construction of a discriminated union variant. * The base type has a discriminator field; we're constructing a specific child. */ export interface VariantConstruct { kind: "variant"; baseTypeName: TypeName; discriminator: string; discriminatorValue: string; variantTypeName: TypeName; fields: FieldAssignment[]; } /** An array/list/vector literal. */ export interface ArrayLiteral { kind: "array"; elementTypeName: TypeName; items: Expr[]; } /** A dictionary/map literal. */ export interface DictLiteral { kind: "dict"; entries: { key: string; value: Expr; }[]; } /** * A field read from a source object. Used in wire format mapping * to read fields from core types (e.g., `opts.maxOutputTokens`). */ export interface FieldRead { kind: "field_read"; /** Source object/variable name (e.g., "opts") */ objectName: string; /** Field name on the source object (e.g., "maxOutputTokens") */ fieldName: string; /** Type of the field (e.g., "int32", "string") */ fieldType: string; /** Whether the field is optional on the source object */ isOptional: boolean; } /** A field assignment within a Construct or VariantConstruct. */ export interface FieldAssignment { /** Original property name from TypeSpec (camelCase). */ propertyName: string; value: Expr; /** Whether the target property is optional (emitters may need wrapping, e.g., Some(), null check) */ isOptional: boolean; } /** The Expression IR — a tagged union with exhaustive pattern matching. */ export type Expr = StringLiteral | NumberLiteral | BooleanLiteral | NullLiteral | ParamRef | Construct | VariantConstruct | ArrayLiteral | DictLiteral | FieldRead; /** * Recursively collect all TypeName references from an Expr tree. * Used by emitters to determine which additional types need importing * when factory/coercion expressions reference types from other modules. */ export declare function collectExprTypeRefs(expr: Expr): TypeName[]; /** * Registry of TypeNodes by name, enabling the resolver to look up types * when processing nested objects and discriminated unions. * * Built from the emitter's type graph (TypeNode tree + enumerateTypes). */ export declare class TypeRegistry { private types; /** Register a type by its simple name. */ register(node: TypeNode): void; /** Look up a type by simple name. Returns undefined if not found. */ get(name: string): TypeNode | undefined; /** Build a registry from a root TypeNode by walking all reachable types. */ static fromTypeGraph(roots: TypeNode[]): TypeRegistry; } /** * Resolve a @factory decorator into a typed Expr tree. * * @param sets - Field assignments from the decorator (e.g., { allowed: true }) * @param params - Parameter declarations (e.g., { reason: "string" }) * @param targetType - The TypeNode this factory constructs * @param registry - Type registry for resolving nested types * @returns A Construct expression representing the factory body */ export declare function resolveFactoryExpr(sets: Record, params: Record, targetType: TypeNode, registry: TypeRegistry): Construct; /** * Resolve a @coerce decorator into a typed Expr tree. * * A coercion is essentially a factory with a single implicit parameter named "value". * The expansion dict maps property names to values, where "{value}" is the parameter ref. * * @param expansion - The expansion dict (e.g., { id: "{value}" }) * @param scalarType - The scalar type string (e.g., "string") * @param targetType - The TypeNode this coercion constructs * @param registry - Type registry for resolving nested types * @returns A Construct expression representing the coercion expansion */ export declare function resolveCoerceExpr(expansion: Record, scalarType: string, targetType: TypeNode, registry: TypeRegistry, paramName?: string): Construct;