Skip to content

Commit

Permalink
Semantic: smarter method static dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 9, 2024
1 parent 729a1b8 commit 8d6ffe4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/codegen/js/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,14 @@ export const emitOperand = (operand: Operand, module: Module, ctx: Context): Emi
return { emit: emitTree([...ts.map(t => t.emit), concatEmit]), resultVar }
case 'identifier': {
if (operand.ref?.def.kind === 'method-def') {
const staticCallEmit = operand.impl ? jsRelName(operand.impl) : undefined
if (operand.ref.def.fn.static === true) {
const typeName = operand.names.at(-2)!.value
const traitName = relTypeName(operand.ref.def.rel)
const callerEmit = staticCallEmit ?? `${typeName}.${traitName}`
return {
emit: emitToken(''),
resultVar: `${typeName}.${traitName}().${operand.ref.def.fn.name.value}`
resultVar: `${callerEmit}().${operand.ref.def.fn.name.value}`
}
} else {
const args = operand.ref.def.fn.params.map((_, i) => {
Expand All @@ -308,11 +310,7 @@ export const emitOperand = (operand: Operand, module: Module, ctx: Context): Emi
})
const relName = jsRelName(operand.ref.def.rel)
const fnName = operand.ref.def.fn.name.value
// TODO: this is probably wrong
const callerEmit =
operand.impl && operand.impl.instanceDef.kind === 'impl-def'
? jsRelName(operand.impl)
: `${args[0].resultVar}.${relName}`
const callerEmit = staticCallEmit ?? `${args[0].resultVar}.${relName}`
const delegate = `return ${callerEmit}().${fnName}(${args.map(a => a.resultVar)});`
const block = `${args.map(a => (<EmitToken>a.emit).value)}${delegate}`
return {
Expand Down
6 changes: 4 additions & 2 deletions src/semantic/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,10 @@ export const checkQualifiedMethodCall = (
}
}
} else {
impl = ref.def.rel
ctx.moduleStack.at(-1)!.relImports.push(impl)
if (ref.def.fn.block) {
impl = ref.def.rel
ctx.moduleStack.at(-1)!.relImports.push(impl)
}
}
identifier.impl = impl

Expand Down

0 comments on commit 8d6ffe4

Please sign in to comment.