summary refs log tree commit diff stats
path: root/compiler/semcall.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/semcall.nim')
-rwxr-xr-xcompiler/semcall.nim107
1 files changed, 58 insertions, 49 deletions
diff --git a/compiler/semcall.nim b/compiler/semcall.nim
index c92eff319..f0c9f42b0 100755
--- a/compiler/semcall.nim
+++ b/compiler/semcall.nim
@@ -8,6 +8,7 @@
 #
 
 ## This module implements semantic checking for calls. 
+# included from sem.nim
 
 proc sameMethodDispatcher(a, b: PSym): bool = 
   result = false
@@ -17,70 +18,78 @@ proc sameMethodDispatcher(a, b: PSym): bool =
     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 = 
+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
-    x, y, z: TCandidate
+    alt, z: TCandidate
+
+  template best: expr = result
   #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)
+  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)
+  while sym != nil:
+    if sym.kind in filter:
+      initCandidate(z, sym, initialBinding, o.lastOverloadScope)
       z.calleeSym = sym
-      matches(c, n, z)
-      if z.state == csMatch: 
+      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 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
+        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 x.state == csEmpty: 
+
+  if best.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") 
+  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")
     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)
+      getProcHeader(best.calleeSym), getProcHeader(alt.calleeSym),
+      best.calleeSym.Name.s])
+
+proc semResolvedCall(c: PContext, n: PNode, x: TCandidate): PNode =
+  assert x.state == csMatch
+  var finalCallee = x.calleeSym
+  markUsed(n, finalCallee)
+  if finalCallee.ast == nil:
+    internalError(n.info, "calleeSym.ast is nil") # XXX: remove this check!
+  if finalCallee.ast.sons[genericParamsPos].kind != nkEmpty:
+    # a generic proc!
+    finalCallee = generateInstance(c, x.calleeSym, x.bindings, n.info)
+
+  result = x.call
+  result.sons[0] = newSymNode(finalCallee)
+  result.typ = finalCallee.typ.sons[0]
 
+proc semOverloadedCall(c: PContext, n, nOrig: PNode,
+                       filter: TSymKinds): PNode =
+  var r = resolveOverloads(c, n, nOrig, filter)
+  if r.state == csMatch: result = semResolvedCall(c, n, r)
+    
 proc explicitGenericInstError(n: PNode): PNode =
   LocalError(n.info, errCannotInstantiateX, renderTree(n))
   result = n