From 4181baf400cfb63609cc2c195cfaf0b7a2e75153 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 14 Mar 2019 09:03:21 +0100 Subject: [PATCH] multi-methods: remove hack, make tmethod_various compile under strict C++ [backport] --- compiler/ast.nim | 2 +- compiler/cgmeth.nim | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 7a21f49ff7..f6778eb854 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -996,7 +996,7 @@ const miscPos* = 5 # used for undocumented and hacky stuff bodyPos* = 6 # position of body; use rodread.getBody() instead! resultPos* = 7 - dispatcherPos* = 8 # caution: if method has no 'result' it can be position 7! + dispatcherPos* = 8 nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit, nkHiddenCallConv} diff --git a/compiler/cgmeth.nim b/compiler/cgmeth.nim index f5014fa8ca..3c329ca05e 100644 --- a/compiler/cgmeth.nim +++ b/compiler/cgmeth.nim @@ -37,10 +37,9 @@ proc genConv(n: PNode, d: PType, downcast: bool; conf: ConfigRef): PNode = proc getDispatcher*(s: PSym): PSym = ## can return nil if is has no dispatcher. - let dispn = lastSon(s.ast) - if dispn.kind == nkSym: - let disp = dispn.sym - if sfDispatcher in disp.flags: result = disp + if dispatcherPos < s.ast.len: + result = s.ast[dispatcherPos].sym + doAssert sfDispatcher in result.flags proc methodCall*(n: PNode; conf: ConfigRef): PNode = result = n @@ -99,13 +98,14 @@ proc sameMethodBucket(a, b: PSym): MethodResult = return No proc attachDispatcher(s: PSym, dispatcher: PNode) = - var L = s.ast.len-1 - var x = s.ast.sons[L] - if x.kind == nkSym and sfDispatcher in x.sym.flags: + if dispatcherPos < s.ast.len: # we've added a dispatcher already, so overwrite it - s.ast.sons[L] = dispatcher + s.ast.sons[dispatcherPos] = dispatcher 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 = var disp = copySym(s) @@ -165,7 +165,7 @@ proc methodDef*(g: ModuleGraph; s: PSym, fromCache: bool) = case sameMethodBucket(disp, s) of Yes: add(g.methods[i].methods, s) - attachDispatcher(s, lastSon(disp.ast)) + attachDispatcher(s, disp.ast[dispatcherPos]) fixupDispatcher(s, disp, g.config) #echo "fixup ", disp.name.s, " ", disp.id when useEffectSystem: checkMethodEffects(g, disp, s) @@ -226,7 +226,7 @@ proc sortBucket(a: var seq[PSym], relevantCols: IntSet) = if h == 1: break 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 var paramLen = sonsLen(base.typ) var nilchecks = newNodeI(nkStmtList, base.info)