implemented generic multi methods

This commit is contained in:
Araq
2013-01-16 08:42:30 +01:00
parent c9690864d4
commit c43697b59a
9 changed files with 81 additions and 31 deletions

View File

@@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -65,10 +65,10 @@ proc sameMethodBucket(a, b: PSym): bool =
break
if sameType(aa, bb) or
(aa.kind == tyObject) and (bb.kind == tyObject) and
(inheritanceDiff(bb, aa) < 0):
(inheritanceDiff(bb, aa) < 0):
nil
else:
return
else:
return
result = true
proc attachDispatcher(s: PSym, dispatcher: PNode) =
@@ -106,17 +106,16 @@ proc methodDef*(s: PSym, fromCache: bool) =
# attach to itself to prevent bugs:
attachDispatcher(disp, newSymNode(disp))
proc relevantCol(methods: TSymSeq, col: int): bool =
proc relevantCol(methods: TSymSeq, col: int): bool =
# returns true iff the position is relevant
var t = methods[0].typ.sons[col]
result = false
if skipTypes(t, skipPtrs).kind == tyObject:
for i in countup(1, high(methods)):
if not SameType(methods[i].typ.sons[col], t):
if skipTypes(t, skipPtrs).kind == tyObject:
for i in countup(1, high(methods)):
let t2 = skipTypes(methods[i].typ.sons[col], skipPtrs)
if not SameType(t2, t):
return true
proc cmpSignatures(a, b: PSym, relevantCols: TIntSet): int =
result = 0
for col in countup(1, sonsLen(a.typ) - 1):
if Contains(relevantCols, col):
var aa = skipTypes(a.typ.sons[col], skipPtrs)

View File

@@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -45,6 +45,7 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode
proc fixImmediateParams(n: PNode): PNode
proc activate(c: PContext, n: PNode)
proc semQuoteAst(c: PContext, n: PNode): PNode
proc finishMethod(c: PContext, s: PSym)
proc IndexTypesMatch(c: PContext, f, a: PType, arg: PNode): PNode

View File

@@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -15,8 +15,22 @@ proc sameMethodDispatcher(a, b: PSym): bool =
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
if aa.kind == nkSym and bb.kind == nkSym:
if aa.sym == bb.sym:
result = true
else:
nil
# generics have no dispatcher yet, so we need to compare the method
# names; however, the names are equal anyway because otherwise we
# wouldn't even consider them to be overloaded. But even this does
# not work reliably! See tmultim6 for an example:
# method collide[T](a: TThing, b: TUnit[T]) is instantiated and not
# method collide[T](a: TUnit[T], b: TThing)! This means we need to
# *instantiate* every candidate! However, we don't keep more than 2-3
# candidated around so we cannot implement that for now. So in order
# to avoid subtle problems, the call remains ambiguous and needs to
# be disambiguated by the programmer; this way the right generic is
# instantiated.
proc resolveOverloads(c: PContext, n, orig: PNode,
filter: TSymKinds): TCandidate =

View File

@@ -186,6 +186,7 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
popOwner()
c.friendModule = oldFriend
dec(c.InstCounter)
if result.kind == skMethod: finishMethod(c, result)
proc instGenericContainer(c: PContext, n: PNode, header: PType): PType =
var cl: TReplTypeVars

View File

@@ -867,25 +867,26 @@ proc semIterator(c: PContext, n: PNode): PNode =
proc semProc(c: PContext, n: PNode): PNode =
result = semProcAux(c, n, skProc, procPragmas)
proc hasObjParam(s: PSym): bool =
var t = s.typ
for col in countup(1, sonsLen(t)-1):
if skipTypes(t.sons[col], skipPtrs).kind == tyObject:
return true
proc finishMethod(c: PContext, s: PSym) =
if hasObjParam(s):
methodDef(s, false)
proc semMethod(c: PContext, n: PNode): PNode =
if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "method")
result = semProcAux(c, n, skMethod, methodPragmas)
var s = result.sons[namePos].sym
var t = s.typ
var hasObjParam = false
for col in countup(1, sonsLen(t)-1):
if skipTypes(t.sons[col], skipPtrs).kind == tyObject:
hasObjParam = true
break
# XXX this not really correct way to do it: Perhaps it should be done after
# generic instantiation. Well it's good enough for now:
if hasObjParam:
methodDef(s, false)
else:
LocalError(n.info, errXNeedsParamObjectType, "method")
if not isGenericRoutine(s):
if hasObjParam(s):
methodDef(s, false)
else:
LocalError(n.info, errXNeedsParamObjectType, "method")
proc semConverterDef(c: PContext, n: PNode): PNode =
if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "converter")

View File

@@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
# (c) Copyright 2013 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@@ -876,20 +876,24 @@ proc inheritanceDiff*(a, b: PType): int =
# | returns: -x iff `a` is the x'th direct superclass of `b`
# | returns: +x iff `a` is the x'th direct subclass of `b`
# | returns: `maxint` iff `a` and `b` are not compatible at all
assert a.kind == tyObject
assert b.kind == tyObject
var x = a
result = 0
while x != nil:
x = skipTypes(x, skipPtrs)
if sameObjectTypes(x, b): return
x = x.sons[0]
dec(result)
var y = b
result = 0
while y != nil:
y = skipTypes(y, skipPtrs)
if sameObjectTypes(y, a): return
y = y.sons[0]
inc(result)
result = high(int)
proc typeAllowedAux(marker: var TIntSet, typ: PType, kind: TSymKind): bool
proc typeAllowedNode(marker: var TIntSet, n: PNode, kind: TSymKind): bool =
result = true

30
tests/run/tmultim6.nim Normal file
View File

@@ -0,0 +1,30 @@
discard """
output: "collide: unit, thing | collide: unit, thing | collide: thing, unit"
"""
# Test multi methods
type
TThing = object {.inheritable.}
TUnit[T] = object of TThing
x: T
TParticle = object of TThing
a, b: int
method collide(a, b: TThing) {.inline.} =
quit "to override!"
method collide[T](a: TThing, b: TUnit[T]) {.inline.} =
write stdout, "collide: thing, unit | "
method collide[T](a: TUnit[T], b: TThing) {.inline.} =
write stdout, "collide: unit, thing | "
proc test(a, b: TThing) {.inline.} =
collide(a, b)
var
a: TThing
b, c: TUnit[string]
collide(b, TThing(c))
test(b, c)
collide(a, b)

View File

@@ -15,7 +15,6 @@ version 0.9.X
- test&finish first class iterators:
* nested iterators
- implement the missing features wrt inheritance
- implement generic methods
- improve the compiler as a service
- ``=`` should be overloadable; requires specialization for ``=``
- implement constructors + full 'not nil' checking

View File

@@ -57,6 +57,7 @@ Language Additions
symbol forwarding so client modules don't have to import a module's
dependencies explicitly.
- Overloading based on ASTs has been implemented.
- Generics are now supported for multi methods.
2012-09-23 Version 0.9.0 released