multi-methods: remove hack, make tmethod_various compile under strict C++ [backport]

This commit is contained in:
Andreas Rumpf 2019-03-14 09:03:21 +01:00
commit 4181baf400
2 changed files with 12 additions and 12 deletions

View file

@ -996,7 +996,7 @@ const
miscPos* = 5 # used for undocumented and hacky stuff miscPos* = 5 # used for undocumented and hacky stuff
bodyPos* = 6 # position of body; use rodread.getBody() instead! bodyPos* = 6 # position of body; use rodread.getBody() instead!
resultPos* = 7 resultPos* = 7
dispatcherPos* = 8 # caution: if method has no 'result' it can be position 7! dispatcherPos* = 8
nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix, nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix,
nkCommand, nkCallStrLit, nkHiddenCallConv} nkCommand, nkCallStrLit, nkHiddenCallConv}

View file

@ -37,10 +37,9 @@ proc genConv(n: PNode, d: PType, downcast: bool; conf: ConfigRef): PNode =
proc getDispatcher*(s: PSym): PSym = proc getDispatcher*(s: PSym): PSym =
## can return nil if is has no dispatcher. ## can return nil if is has no dispatcher.
let dispn = lastSon(s.ast) if dispatcherPos < s.ast.len:
if dispn.kind == nkSym: result = s.ast[dispatcherPos].sym
let disp = dispn.sym doAssert sfDispatcher in result.flags
if sfDispatcher in disp.flags: result = disp
proc methodCall*(n: PNode; conf: ConfigRef): PNode = proc methodCall*(n: PNode; conf: ConfigRef): PNode =
result = n result = n
@ -99,13 +98,14 @@ proc sameMethodBucket(a, b: PSym): MethodResult =
return No return No
proc attachDispatcher(s: PSym, dispatcher: PNode) = proc attachDispatcher(s: PSym, dispatcher: PNode) =
var L = s.ast.len-1 if dispatcherPos < s.ast.len:
var x = s.ast.sons[L]
if x.kind == nkSym and sfDispatcher in x.sym.flags:
# we've added a dispatcher already, so overwrite it # we've added a dispatcher already, so overwrite it
s.ast.sons[L] = dispatcher s.ast.sons[dispatcherPos] = dispatcher
else: else:
s.ast.add(dispatcher) setLen(s.ast.sons, dispatcherPos+1)
if s.ast[resultPos] == nil:
s.ast[resultPos] = newNodeI(nkEmpty, s.info)
s.ast.sons[dispatcherPos] = dispatcher
proc createDispatcher(s: PSym): PSym = proc createDispatcher(s: PSym): PSym =
var disp = copySym(s) var disp = copySym(s)
@ -165,7 +165,7 @@ proc methodDef*(g: ModuleGraph; s: PSym, fromCache: bool) =
case sameMethodBucket(disp, s) case sameMethodBucket(disp, s)
of Yes: of Yes:
add(g.methods[i].methods, s) add(g.methods[i].methods, s)
attachDispatcher(s, lastSon(disp.ast)) attachDispatcher(s, disp.ast[dispatcherPos])
fixupDispatcher(s, disp, g.config) fixupDispatcher(s, disp, g.config)
#echo "fixup ", disp.name.s, " ", disp.id #echo "fixup ", disp.name.s, " ", disp.id
when useEffectSystem: checkMethodEffects(g, disp, s) when useEffectSystem: checkMethodEffects(g, disp, s)
@ -226,7 +226,7 @@ proc sortBucket(a: var seq[PSym], relevantCols: IntSet) =
if h == 1: break if h == 1: break
proc genDispatcher(g: ModuleGraph; methods: seq[PSym], relevantCols: IntSet): PSym = proc genDispatcher(g: ModuleGraph; methods: seq[PSym], relevantCols: IntSet): PSym =
var base = lastSon(methods[0].ast).sym var base = methods[0].ast[dispatcherPos].sym
result = base result = base
var paramLen = sonsLen(base.typ) var paramLen = sonsLen(base.typ)
var nilchecks = newNodeI(nkStmtList, base.info) var nilchecks = newNodeI(nkStmtList, base.info)