refactoring: suggest can import sigmatch for type matching
This commit is contained in:
parent
4e7a22cac3
commit
fdde4d3a92
12 changed files with 445 additions and 266 deletions
121
rod/sigmatch.nim
121
rod/sigmatch.nim
|
|
@ -10,24 +10,28 @@
|
|||
# This module implements the signature matching for resolving
|
||||
# the call to overloaded procs, generic procs and operators.
|
||||
|
||||
import
|
||||
ast, astalgo, semdata, types, msgs, rnimsyn, lookups, semtypinst,
|
||||
magicsys
|
||||
|
||||
type
|
||||
TCandidateState = enum
|
||||
TCandidateState* = enum
|
||||
csEmpty, csMatch, csNoMatch
|
||||
TCandidate{.final.} = object
|
||||
TCandidate* {.final.} = object
|
||||
exactMatches: int
|
||||
subtypeMatches: int
|
||||
intConvMatches: int # conversions to int are not as expensive
|
||||
convMatches: int
|
||||
genericMatches: int
|
||||
state: TCandidateState
|
||||
callee: PType # may not be nil!
|
||||
calleeSym: PSym # may be nil
|
||||
call: PNode # modified call
|
||||
bindings: TIdTable # maps sym-ids to types
|
||||
state*: TCandidateState
|
||||
callee*: PType # may not be nil!
|
||||
calleeSym*: PSym # may be nil
|
||||
call*: PNode # modified call
|
||||
bindings*: TIdTable # maps sym-ids to types
|
||||
baseTypeMatch: bool # needed for conversions from T to openarray[T]
|
||||
# for example
|
||||
|
||||
TTypeRelation = enum # order is important!
|
||||
TTypeRelation* = enum # order is important!
|
||||
isNone, isConvertible, isIntConv, isSubtype, isGeneric, isEqual
|
||||
|
||||
proc initCandidateAux(c: var TCandidate, callee: PType) {.inline.} =
|
||||
|
|
@ -41,12 +45,12 @@ proc initCandidateAux(c: var TCandidate, callee: PType) {.inline.} =
|
|||
c.call = nil
|
||||
c.baseTypeMatch = false
|
||||
|
||||
proc initCandidate(c: var TCandidate, callee: PType) =
|
||||
proc initCandidate*(c: var TCandidate, callee: PType) =
|
||||
initCandidateAux(c, callee)
|
||||
c.calleeSym = nil
|
||||
initIdTable(c.bindings)
|
||||
|
||||
proc initCandidate(c: var TCandidate, callee: PSym, binding: PNode) =
|
||||
proc initCandidate*(c: var TCandidate, callee: PSym, binding: PNode) =
|
||||
initCandidateAux(c, callee.typ)
|
||||
c.calleeSym = callee
|
||||
initIdTable(c.bindings)
|
||||
|
|
@ -70,7 +74,7 @@ proc copyCandidate(a: var TCandidate, b: TCandidate) =
|
|||
a.baseTypeMatch = b.baseTypeMatch
|
||||
copyIdTable(a.bindings, b.bindings)
|
||||
|
||||
proc cmpCandidates(a, b: TCandidate): int =
|
||||
proc cmpCandidates*(a, b: TCandidate): int =
|
||||
result = a.exactMatches - b.exactMatches
|
||||
if result != 0: return
|
||||
result = a.genericMatches - b.genericMatches
|
||||
|
|
@ -88,7 +92,7 @@ proc writeMatches(c: TCandidate) =
|
|||
Writeln(stdout, "intconv matches: " & $c.intConvMatches)
|
||||
Writeln(stdout, "generic matches: " & $c.genericMatches)
|
||||
|
||||
proc getNotFoundError(c: PContext, n: PNode): string =
|
||||
proc getNotFoundError*(c: PContext, n: PNode): string =
|
||||
# Gives a detailed error message; this is separated from semDirectCall,
|
||||
# as semDirectCall is already pretty slow (and we need this information only
|
||||
# in case of an error).
|
||||
|
|
@ -425,7 +429,7 @@ proc typeRel(mapping: var TIdTable, f, a: PType): TTypeRelation =
|
|||
else: nil
|
||||
else: internalError("typeRel(" & $f.kind & ')')
|
||||
|
||||
proc cmpTypes(f, a: PType): TTypeRelation =
|
||||
proc cmpTypes*(f, a: PType): TTypeRelation =
|
||||
var mapping: TIdTable
|
||||
InitIdTable(mapping)
|
||||
result = typeRel(mapping, f, a)
|
||||
|
|
@ -552,7 +556,7 @@ proc ParamTypesMatch(c: PContext, m: var TCandidate, f, a: PType,
|
|||
markUsed(arg, arg.sons[best].sym)
|
||||
result = ParamTypesMatchAux(c, m, f, arg.sons[best].typ, arg.sons[best])
|
||||
|
||||
proc IndexTypesMatch(c: PContext, f, a: PType, arg: PNode): PNode =
|
||||
proc IndexTypesMatch*(c: PContext, f, a: PType, arg: PNode): PNode =
|
||||
var m: TCandidate
|
||||
initCandidate(m, f)
|
||||
result = paramTypesMatch(c, m, f, a, arg)
|
||||
|
|
@ -561,7 +565,7 @@ proc setSon(father: PNode, at: int, son: PNode) =
|
|||
if sonsLen(father) <= at: setlen(father.sons, at + 1)
|
||||
father.sons[at] = son
|
||||
|
||||
proc matches(c: PContext, n: PNode, m: var TCandidate) =
|
||||
proc matches*(c: PContext, n: PNode, m: var TCandidate) =
|
||||
var f = 1 # iterates over formal parameters
|
||||
var a = 1 # iterates over the actual given arguments
|
||||
m.state = csMatch # until proven otherwise
|
||||
|
|
@ -670,90 +674,3 @@ proc matches(c: PContext, n: PNode, m: var TCandidate) =
|
|||
setSon(m.call, formal.position + 1, copyTree(formal.ast))
|
||||
inc(f)
|
||||
|
||||
proc sameMethodDispatcher(a, b: PSym): bool =
|
||||
result = false
|
||||
if a.kind == skMethod and b.kind == skMethod:
|
||||
var aa = lastSon(a.ast)
|
||||
var bb = lastSon(b.ast)
|
||||
if aa.kind == nkSym and bb.kind == nkSym and aa.sym == bb.sym:
|
||||
result = true
|
||||
|
||||
proc semDirectCallWithBinding(c: PContext, n, f: PNode, filter: TSymKinds,
|
||||
initialBinding: PNode): PNode =
|
||||
var
|
||||
o: TOverloadIter
|
||||
x, y, z: TCandidate
|
||||
#Message(n.info, warnUser, renderTree(n))
|
||||
var sym = initOverloadIter(o, c, f)
|
||||
result = nil
|
||||
if sym == nil: return
|
||||
initCandidate(x, sym, initialBinding)
|
||||
initCandidate(y, sym, initialBinding)
|
||||
|
||||
while sym != nil:
|
||||
if sym.kind in filter:
|
||||
initCandidate(z, sym, initialBinding)
|
||||
z.calleeSym = sym
|
||||
matches(c, n, z)
|
||||
if z.state == csMatch:
|
||||
case x.state
|
||||
of csEmpty, csNoMatch: x = z
|
||||
of csMatch:
|
||||
var cmp = cmpCandidates(x, z)
|
||||
if cmp < 0: x = z # z is better than x
|
||||
elif cmp == 0: y = z # z is as good as x
|
||||
else: nil
|
||||
sym = nextOverloadIter(o, c, f)
|
||||
if x.state == csEmpty:
|
||||
# no overloaded proc found
|
||||
# do not generate an error yet; the semantic checking will check for
|
||||
# an overloaded () operator
|
||||
elif y.state == csMatch and cmpCandidates(x, y) == 0 and
|
||||
not sameMethodDispatcher(x.calleeSym, y.calleeSym):
|
||||
if x.state != csMatch:
|
||||
InternalError(n.info, "x.state is not csMatch")
|
||||
LocalError(n.Info, errGenerated, msgKindToString(errAmbiguousCallXYZ) % [
|
||||
getProcHeader(x.calleeSym), getProcHeader(y.calleeSym),
|
||||
x.calleeSym.Name.s])
|
||||
else:
|
||||
# only one valid interpretation found:
|
||||
markUsed(n, x.calleeSym)
|
||||
if x.calleeSym.ast == nil:
|
||||
internalError(n.info, "calleeSym.ast is nil") # XXX: remove this check!
|
||||
if x.calleeSym.ast.sons[genericParamsPos].kind != nkEmpty:
|
||||
# a generic proc!
|
||||
x.calleeSym = generateInstance(c, x.calleeSym, x.bindings, n.info)
|
||||
x.callee = x.calleeSym.typ
|
||||
result = x.call
|
||||
result.sons[0] = newSymNode(x.calleeSym)
|
||||
result.typ = x.callee.sons[0]
|
||||
|
||||
proc semDirectCall(c: PContext, n: PNode, filter: TSymKinds): PNode =
|
||||
# process the bindings once:
|
||||
var initialBinding: PNode
|
||||
var f = n.sons[0]
|
||||
if f.kind == nkBracketExpr:
|
||||
# fill in the bindings:
|
||||
initialBinding = f
|
||||
f = f.sons[0]
|
||||
else:
|
||||
initialBinding = nil
|
||||
result = semDirectCallWithBinding(c, n, f, filter, initialBinding)
|
||||
|
||||
proc explictGenericInstantiation(c: PContext, n: PNode, s: PSym): PNode =
|
||||
assert n.kind == nkBracketExpr
|
||||
for i in 1..sonsLen(n)-1:
|
||||
n.sons[i].typ = semTypeNode(c, n.sons[i], nil)
|
||||
# we cannot check for the proper number of type parameters because in
|
||||
# `f[a,b](x, y)` `f` is not resolved yet properly.
|
||||
# XXX: BUG this should be checked somehow!
|
||||
assert n.sons[0].kind == nkSym
|
||||
|
||||
var x: TCandidate
|
||||
initCandidate(x, s, n)
|
||||
var newInst = generateInstance(c, s, x.bindings, n.info)
|
||||
|
||||
markUsed(n, s)
|
||||
result = newSymNode(newInst)
|
||||
result.info = n.info
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue