/** * Cross-module lookup helpers: declaration resolution against the * wasm module's discovery state and value-export collection. Kept * out of codegen-utils to avoid an import cycle (wasm-module imports * codegen-utils). */ import { ClassDeclaration, DeclareFunction, EnumDeclaration, ExportAllDeclaration, ExportFromDeclaration, IdentifierPattern, Node, VariableDeclaration, FunctionDeclaration, } from '../ast.zena'; import { ArrayType, ClassType, FunctionType, InterfaceType, Type, TypeParameterType, } from '../types.zena'; import { Symbol } from '../scope/symbol.zena'; import { Program, CompilationUnit } from '../program.zena'; import { WasmModule } from './wasm-module.zena'; import { dirName, normalizePath } from '../module-resolver.zena'; import { getTypeUniqueKey } from './codegen-utils.zena'; import { HashMap } from 'zena:collections'; function matchTypes(t1: Type, t2: Type): boolean { if (t1 is TypeParameterType) { return true; } match (t1) { case ArrayType {elementType}: { if (t2 is ArrayType) { return matchTypes(elementType, t2.elementType); } return false; } case ClassType as ct1: { if (t2 is ClassType) { if (ct1.name != t2.name) { return false; } let {typeArguments} = ct1; if (typeArguments != null && t2.typeArguments != null) { let args1 = typeArguments; let args2 = t2.typeArguments as Array; if (args1.length != args2.length) { return false; } var i = 0; while (i < args1.length) { if (!matchTypes(args1[i], args2[i])) { return false; } i += 1; } } return true; } return false; } case InterfaceType as it1: { if (t2 is InterfaceType) { if (it1.name != t2.name) { return false; } let {typeArguments} = it1; if (typeArguments != null && t2.typeArguments != null) { let args1 = typeArguments; let args2 = t2.typeArguments as Array; if (args1.length != args2.length) { return false; } var i = 0; while (i < args1.length) { if (!matchTypes(args1[i], args2[i])) { return false; } i += 1; } } return true; } return false; } case _: { return getTypeUniqueKey(t1) == getTypeUniqueKey(t2); } } } function matchOverload(t1: Type, t2: Type): boolean { if (t1 is FunctionType && t2 is FunctionType) { if (t1.parameters.length != t2.parameters.length) { return false; } var i = 0; while (i < t1.parameters.length) { if (!matchTypes(t1.parameters[i], t2.parameters[i])) { return false; } i += 1; } return true; } return matchTypes(t1, t2); } export function findDeclaration(wasm: WasmModule, sym: Symbol, expectedType: Type | null): DeclareFunction | null { let resolved = sym.resolveTarget(); let decl = resolved.declaration; if (decl == null) return null; let prog = wasm.program; if (prog != null && resolved.modulePath != "") { let targetUnit = prog.unitFor(resolved.modulePath); if (targetUnit != null) { let importedName = resolved.name; // Keyed by the unit this compile linked, not by the path the // symbol was declared under. For a virtual library those differ // the moment a check result is shared between targets: the // symbol names the implementation its own compile resolved, and // this compile has another library under that name. let key = `${targetUnit.ast.path}::${importedName}`; if (let (true, list) = prog.exportedFunctions.get(key)) { var fallback: DeclareFunction | null = null; for (let df in list) { if (expectedType == null) { return df; } let nodeType = targetUnit.model.getNodeType(df); if (nodeType != null && matchOverload(nodeType, expectedType)) { return df; } fallback = df; } if (fallback != null) { return fallback; } } } } if (decl is DeclareFunction) { return decl; } return null; } export function collectValueExports(program: Program, unit: CompilationUnit, result: HashMap): void { for (let stmt in unit.ast.body) { match (stmt) { case VariableDeclaration {pattern, exported}: { if (exported && pattern is IdentifierPattern) { let sym = unit.model.resolveDeclaration(pattern); if (sym != null) { result[pattern.name] = sym; } } } case FunctionDeclaration {name as namePat, exported}: { if (exported) { let sym = unit.model.resolveDeclaration(namePat as Node); if (sym != null) { result[namePat.name] = sym; } } } case ClassDeclaration {name, exported}: { if (exported) { let sym = unit.model.resolveDeclaration(name); if (sym != null) { result[name.name] = sym; } } } case EnumDeclaration {name, exported}: { if (exported) { let sym = unit.model.resolveDeclaration(name); if (sym != null) { result[name.name] = sym; } } } case DeclareFunction {name, exported}: { if (exported) { let sym = unit.model.resolveDeclaration(name); if (sym != null) { result[name.name] = sym; } } } case ExportFromDeclaration {specifiers, moduleSpecifier}: { let specifier = moduleSpecifier.value; var canonicalPath = specifier; if (specifier.startsWith("./") || specifier.startsWith("../")) { let dir = dirName(unit.ast.path); canonicalPath = normalizePath(`${dir}/${specifier}`); } if (program.unitsByPath.has(canonicalPath)) { let targetUnit = program.unitsByPath[canonicalPath]; for (let spec in specifiers) { let importedName = spec.imported.name; let localName = spec.local.name; let targetExports = new Map(); collectValueExports(program, targetUnit, targetExports); if (targetExports.has(importedName)) { result[localName] = targetExports[importedName]; } } } } case ExportAllDeclaration {moduleSpecifier}: { let specifier = moduleSpecifier.value; var canonicalPath = specifier; if (specifier.startsWith("./") || specifier.startsWith("../")) { let dir = dirName(unit.ast.path); canonicalPath = normalizePath(`${dir}/${specifier}`); } if (program.unitsByPath.has(canonicalPath)) { let targetUnit = program.unitsByPath[canonicalPath]; collectValueExports(program, targetUnit, result); } } case _: {} } } } export function isArrayIntrinsic(name: String): boolean { return name == "__array_len" || name == "__byte_array_length" || name == "__array_get" || name == "__byte_array_get" || name == "__array_set" || name == "__byte_array_set" || name == "__array_new" || name == "__array_new_empty" || name == "__byte_array_new" || name == "__byte_array_copy"; }