# # # The Nimrod Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## This module implements semantic checking for calls. # included from sem.nim 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 resolveOverloads(c: PContext, n, orig: PNode, filter: TSymKinds): TCandidate = 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 var o: TOverloadIter alt, z: TCandidate template best: expr = result #Message(n.info, warnUser, renderTree(n)) var sym = initOverloadIter(o, c, f) var symScope = o.lastOverloadScope if sym == nil: return initCandidate(best, sym, initialBinding, symScope) initCandidate(alt, sym, initialBinding, symScope) while sym != nil: if sym.kind in filter: initCandidate(z, sym, initialBinding, o.lastOverloadScope) z.calleeSym = sym matches(c, n, orig, z) if z.state == csMatch: # little hack so that iterators are preferred over everything else: if sym.kind == skIterator: inc(z.exactMatches, 200) case best.state of csEmpty, csNoMatch: best = z of csMatch: var cmp = cmpCandidates(best, z) if cmp < 0: best = z # x is better than the best so far elif cmp == 0: alt = z # x is as good as the best so far else: nil sym = nextOverloadIter(o, c, f) if best.state == csEmpty: # no overloaded proc found # do not generate an error yet; the semantic checking will check for # an overloaded () operator elif alt.state == csMatch and cmpCandidates(best, alt) == 0 and not sameMethodDispatcher(best.calleeSym, alt.calleeSym): if best.state != csMatch: InternalError(n.info, "x.state is not csMatch") #writeMatches(best) #writeMatches(alt) if c.inCompilesContext > 0: # quick error message for performance of 'compiles' built-in: GlobalError(n.Info, errAmbiguousCallXYZ, "") elif gErrorCounter == 0: # don't cascade errors var args = "(" for i in countup(1, sonsLen(n) - 1): if i > 1: add(args, ", ") add(args, typeToString(n.sons[i].typ)) add(args, ")") LocalError(n.Info, errGenerated, msgKindToString(errAmbiguousCallXYZ) % [ getProcHeader(best.calleeSym), getProcHeader(alt.calleeSym), args]) proc instantiateGenericConverters(c: PContext, n: PNode, x: TCandidate) {. noinline.}= for i in 1 ..