# # # The Nim Compiler # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## This module implements lifting for type-bound operations ## (``=sink``, ``=``, ``=destroy``, ``=deepCopy``). # included from sem.nim type TLiftCtx = object c: PContext info: TLineInfo # for construction kind: TTypeAttachedOp fn: PSym asgnForType: PType recurse: bool proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode) proc liftBody(c: PContext; typ: PType; kind: TTypeAttachedOp; info: TLineInfo): PSym {.discardable.} proc at(a, i: PNode, elemType: PType): PNode = result = newNodeI(nkBracketExpr, a.info, 2) result.sons[0] = a result.sons[1] = i result.typ = elemType proc liftBodyTup(c: var TLiftCtx; t: PType; body, x, y: PNode) = for i in 0 ..< t.len: let lit = lowerings.newIntLit(c.c.graph, x.info, i) liftBodyAux(c, t.sons[i], body, x.at(lit, t.sons[i]), y.at(lit, t.sons[i])) proc dotField(x: PNode, f: PSym): PNode = result = newNodeI(nkDotExpr, x.info, 2) result.sons[0] = x result.sons[1] = newSymNode(f, x.info) result.typ = f.typ proc liftBodyObj(c: var TLiftCtx; n, body, x, y: PNode) = case n.kind of nkSym: let f = n.sym liftBodyAux(c, f.typ, body, x.dotField(f), y.dotField(f)) of nkNilLit: discard of nkRecCase: if c.kind in {attachedSink, attachedAsgn, attachedDeepCopy}: ## the value needs to be destroyed before we assign the selector ## or the value is lost let prevKind = c.kind c.kind = attachedDestructor liftBodyObj(c, n, body, x, y) c.kind = prevKind # copy the selector: liftBodyObj(c, n[0], body, x, y) # we need to generate a case statement: var caseStmt = newNodeI(nkCaseStmt, c.info) # XXX generate 'if' that checks same branches # generate selector: var access = dotField(x, n[0].sym) caseStmt.add(access) # copy the branches over, but replace the fields with the for loop body: for i in 1 ..< n.len: var branch = copyTree(n[i]) let L = branch.len branch.sons[L-1] = newNodeI(nkStmtList, c.info) liftBodyObj(c, n[i].lastSon, branch.sons[L-1], x, y) caseStmt.add(branch) body.add(caseStmt) of nkRecList: for t in items(n): liftBodyObj(c, t, body, x, y) else: illFormedAstLocal(n, c.c.config) proc genAddr(c: PContext; x: PNode): PNode = if x.kind == nkHiddenDeref: checkSonsLen(x, 1, c.config) result = x.sons[0] else: result = newNodeIT(nkHiddenAddr, x.info, makeVarType(c, x.typ)) addSon(result, x) proc newAsgnCall(c: PContext; op: PSym; x, y: PNode): PNode = #if sfError in op.flags: # localError(c.config, x.info, "usage of '$1' is a user-defined error" % op.name.s) result = newNodeI(nkCall, x.info) result.add newSymNode(op) result.add genAddr(c, x) result.add y proc newAsgnStmt(le, ri: PNode): PNode = result = newNodeI(nkAsgn, le.info, 2) result.sons[0] = le result.sons[1] = ri proc newOpCall(op: PSym; x: PNode): PNode = result = newNodeIT(nkCall, x.info, op.typ.sons[0]) result.add(newSymNode(op)) result.add x proc destructorCall(c: PContext; op: PSym; x: PNode): PNode = result = newNodeIT(nkCall, x.info, op.typ.sons[0]) result.add(newSymNode(op)) result.add genAddr(c, x) proc newDeepCopyCall(op: PSym; x, y: PNode): PNode = result = newAsgnStmt(x, newOpCall(op, y)) proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode; field: PSym): bool = if tfHasAsgn in t.flags: var op: PSym if sameType(t, c.asgnForType): # generate recursive call: if c.recurse: op = c.fn else: c.recurse = true return false else: op = field if op == nil: op = liftBody(c.c, t, c.kind, c.info) if sfError in op.flags: incl c.fn.flags, sfError else: markUsed(c.c.config, c.info, op, c.c.graph.usageSym) onUse(c.info, op) body.add newAsgnCall(c.c, op, x, y) result = true proc considerOverloadedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool = case c.kind of attachedDestructor: let op = t.destructor if op != nil: markUsed(c.c.config, c.info, op, c.c.graph.usageSym) onUse(c.info, op) body.add destructorCall(c.c, op, x) result = true of attachedAsgn: result = considerAsgnOrSink(c, t, body, x, y, t.assignment) of attachedSink: result = considerAsgnOrSink(c, t, body, x, y, t.sink) of attachedDeepCopy: let op = t.deepCopy if op != nil: markUsed(c.c.config, c.info, op, c.c.graph.usageSym) onUse(c.info, op) body.add newDeepCopyCall(op, x, y) result = true proc defaultOp(c: var TLiftCtx; t: PType; body, x, y: PNode) = if c.kind != attachedDestructor: body.add newAsgnStmt(x, y) proc addVar(father, v, value: PNode) = var vpart = newNodeI(nkIdentDefs, v.info, 3) vpart.sons[0] = v vpart.sons[1] = newNodeI(nkEmpty, v.info) vpart.sons[2] = value addSon(father, vpart) proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode = var temp = newSym(skTemp, getIdent(c.c.cache, lowerings.genPrefix), c.fn, c.info) temp.typ = getSysType(c.c.graph, body.info, tyInt) incl(temp.flags, sfFromGeneric) var v = newNodeI(nkVarSection, c.info) result = newSymNode(temp) v.addVar(result, lowerings.newIntLit(c.c.graph, body.info, first)) body.add v proc genBuiltin(g: ModuleGraph; magic: TMagic; name: string; i: PNode): PNode = result = newNodeI(nkCall, i.info) result.add createMagic(g, name, magic).newSymNode result.add i proc genWhileLoop(c: var TLiftCtx; i, dest: PNode): PNode = result = newNodeI(nkWhileStmt, c.info, 2) let cmp = genBuiltin(c.c.graph, mLeI, "<=", i) cmp.add genHigh(c.c.graph, dest) cmp.typ = getSysType(c.c.graph, c.info, tyBool) result.sons[0] = cmp result.sons[1] = newNodeI(nkStmtList, c.info) proc addIncStmt(c: var TLiftCtx; body, i: PNode) = let incCall = genBuiltin(c.c.graph, mInc, "inc", i) incCall.add lowerings.newIntLit(c.c.graph, c.info, 1) body.add incCall proc newSeqCall(c: PContext; x, y: PNode): PNode = # don't call genAddr(c, x) here: result = genBuiltin(c.graph, mNewSeq, "newSeq", x) let lenCall = genBuiltin(c.graph, mLengthSeq, "len", y) lenCall.typ = getSysType(c.graph, x.info, tyInt) result.add lenCall proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode) = case t.kind of tyNone, tyEmpty, tyVoid: discard of tyPointer, tySet, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString, tyPtr, tyRef, tyOpt, tyUncheckedArray: defaultOp(c, t, body, x, y) of tyArray: if tfHasAsgn in t.flags: let i = declareCounter(c, body, firstOrd(c.c.config, t)) let whileLoop = genWhileLoop(c, i, x) let elemType = t.lastSon liftBodyAux(c, elemType, whileLoop.sons[1], x.at(i, elemType), y.at(i, elemType)) addIncStmt(c, whileLoop.sons[1], i) body.add whileLoop else: defaultOp(c, t, body, x, y) of tySequence: # note that tfHasAsgn is propagated so we need the check on # 'selectedGC' here to determine if we have the new runtime. if c.c.config.selectedGC == gcDestructors: discard considerOverloadedOp(c, t, body, x, y) elif tfHasAsgn in t.flags: if c.kind != attachedDestructor: body.add newSeqCall(c.c, x, y) let i = declareCounter(c, body, firstOrd(c.c.config, t)) let whileLoop = genWhileLoop(c, i, x) let elemType = t.lastSon liftBodyAux(c, elemType, whileLoop.sons[1], x.at(i, elemType), y.at(i, elemType)) addIncStmt(c, whileLoop.sons[1], i) body.add whileLoop else: defaultOp(c, t, body, x, y) of tyString: if tfHasAsgn in t.flags: discard considerOverloadedOp(c, t, body, x, y) else: defaultOp(c, t, body, x, y) of tyObject: if not considerOverloadedOp(c, t, body, x, y): liftBodyObj(c, t.n, body, x, y) of tyDistinct: if not considerOverloadedOp(c, t, body, x, y): liftBodyAux(c, t.sons[0].skipTypes(skipPtrs), body, x, y) of tyTuple: liftBodyTup(c, t, body, x, y) of tyProc: if t.callConv != ccClosure or c.kind != attachedDeepCopy: defaultOp(c, t, body, x, y) else: # a big problem is that we don't know the enviroment's type here, so we # have to go through some indirection; we delegate this to the codegen: let call = newNodeI(nkCall, c.info, 2) call.typ = t call.sons[0] = newSymNode(createMagic(c.c.graph, "deepCopy", mDeepCopy)) call.sons[1] = y