-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify type bounds: resolve method call with impl lookup
- Loading branch information
1 parent
b547a64
commit 55bcc30
Showing
2 changed files
with
57 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
import { MethodCallOp } from '../ast/op' | ||
import { FnDef, TraitDef } from '../ast/statement' | ||
import { Context } from '../scope' | ||
import { InferredType } from '../typecheck' | ||
import { dedup } from '../util/array' | ||
import { assert, todo, unreachable } from '../util/todo' | ||
import { todo } from '../util/todo' | ||
|
||
/** | ||
* TODO(perf): track implemented traits for a type in typeDef.impldTraits, populate it in a separate phase | ||
*/ | ||
export const findMethodDefForMethodCall = (type: InferredType, ctx: Context): FnDef | undefined => { | ||
if (type.kind !== 'method-call') { | ||
assert(false, type.kind) | ||
return unreachable() | ||
} | ||
if (type.operandType.kind === 'identifier') { | ||
const def = type.operandType.def | ||
const impls = ctx.packages.flatMap(p => | ||
p.modules.flatMap(m => m.impls).filter(impl => impl.forTrait && impl.forTrait.def === def) | ||
) | ||
const impldTraits = dedup( | ||
impls | ||
.map(impl => impl.identifier) | ||
.filter(i => i.def && i.def.kind === 'trait-def') | ||
.map(i => <TraitDef>i.def) | ||
) | ||
const matchingFns = impldTraits.flatMap(t => | ||
t.block.statements | ||
.filter(s => s.kind === 'fn-def' && s.name.value === type.op.name.value) | ||
.map(s => <FnDef>s) | ||
) | ||
if (matchingFns.length === 1) { | ||
return matchingFns[0] | ||
} else { | ||
// TODO: error | ||
return undefined | ||
} | ||
} | ||
if (type.operandType.kind === 'type-param') { | ||
// TODO | ||
export const findMethodDefForMethodCall = ( | ||
operandType: InferredType, | ||
op: MethodCallOp, | ||
ctx: Context | ||
): FnDef | undefined => { | ||
if (operandType.kind !== 'identifier') return todo(operandType.kind) | ||
|
||
const def = operandType.def | ||
const block = def?.kind === 'type-def' ? def?.impl?.block : def?.kind === 'trait-def' ? def?.block : undefined | ||
const m = <FnDef>block?.statements.find(s => s.kind === 'fn-def' && s.name.value === op.name.value) | ||
if (m) return m | ||
|
||
const impls = ctx.packages.flatMap(p => | ||
p.modules.flatMap(m => m.impls).filter(impl => impl.forTrait && impl.forTrait.def === def) | ||
) | ||
const impldTraits = dedup( | ||
impls | ||
.map(impl => impl.identifier) | ||
.filter(i => i.def && i.def.kind === 'trait-def') | ||
.map(i => <TraitDef>i.def) | ||
) | ||
const matchingFns = impldTraits.flatMap(t => | ||
t.block.statements.filter(s => s.kind === 'fn-def' && s.name.value === op.name.value).map(s => <FnDef>s) | ||
) | ||
if (matchingFns.length === 1) { | ||
return matchingFns[0] | ||
} else { | ||
// TODO: error | ||
return undefined | ||
} | ||
return todo(type.operandType.kind) | ||
} |