import { Program, CompilationUnit } from '../../program.zena'; import { SemanticModel } from '../../semantic-model.zena'; import { ImportSpecifier, ImportDeclaration, Module } from '../../ast.zena'; import { Type, FunctionType } from '../../types.zena'; import { Symbol, SymbolId, noSymbolId } from '../../scope/symbol.zena'; import { dirName, normalizePath } from '../../module-resolver.zena'; function matchOverloadForImport(fnType: Type, expectedType: Type): boolean { if (!(fnType is FunctionType) || !(expectedType is FunctionType)) { return false; } return fnType.parameters.length == expectedType.parameters.length; } export function resolveImportedSymbolId(program: Program, model: SemanticModel, spec: ImportSpecifier, expectedType: Type | null = null): SymbolId { var parentNode = model.getParent(spec); if (parentNode == null) { for (let u in program.units) { if (u.model == model || u.ast.path == model.sourcePath) { for (let stmt in u.ast.body) { if (stmt is ImportDeclaration) { for (let s in stmt.imports) { if (s == spec || s.imported.name == spec.imported.name) { parentNode = stmt; break; } } } } break; } } } if (parentNode == null || !(parentNode is ImportDeclaration)) { return noSymbolId; } let specifier = parentNode.moduleSpecifier.value; var canonicalPath = specifier; if (specifier.startsWith("./") || specifier.startsWith("../")) { let referrer = model.getParent(parentNode); if (referrer is Module) { let dir = dirName(referrer.path); canonicalPath = normalizePath(`${dir}/${specifier}`); } } // Exact only. `Program` indexes every unit under its canonical path and, // for a stdlib unit, under the module name the manifest gives it, so an // import that resolves at all resolves here. Searching for a unit whose // file name ends the same way used to stand in for that index being // wrong, and matched any path with the right last segment. let targetUnitOpt: CompilationUnit | null = if (program.unitsByPath.has(canonicalPath)) program.unitsByPath[canonicalPath] else null; if (targetUnitOpt == null) { // Not an error: a module outside this program's units is one whose // symbols nothing here can reach, and the caller treats an unresolved // import as nothing to root. return noSymbolId; } let targetUnit = targetUnitOpt as CompilationUnit; let importedName = spec.imported.name; let key1 = `${targetUnit.ast.path}::${importedName}`; let key2 = `${canonicalPath}::${importedName}`; let listOpt: GrowableArray | null = if (program.exportedSymbols.has(key1)) { program.exportedSymbols[key1] } else if (program.exportedSymbols.has(key2)) { program.exportedSymbols[key2] } else { null }; if (listOpt != null) { let list = listOpt; var fallback: Symbol | null = null; for (let sym in list) { if (expectedType == null) { return sym.id; } let declNode = sym.declaration; if (declNode != null) { let nodeType = targetUnit.model.getNodeType(declNode); if (nodeType != null && matchOverloadForImport(nodeType, expectedType)) { return sym.id; } } if (fallback == null) { fallback = sym; } } if (fallback != null) { return fallback.id; } } return noSymbolId; }