# # # The Nim Compiler # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # # included from cgen.nim proc leftAppearsOnRightSide(le, ri: PNode): bool = if le != nil: for i in 1.. 0: skipped = true q = q.lastSon if getMagic(q) == mSlice: # magic: pass slice to openArray: if skipped: q = skipConv(n) while q.kind == nkStmtListExpr and q.len > 0: for i in 0.. 1: pl.add(~", ") # beware of 'result = p(result)'. We may need to allocate a temporary: if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri): # Great, we can use 'd': if d.k == locNone: getTemp(p, typ[0], d, needsInit=true) elif d.k notin {locTemp} and not hasNoInit(ri): # reset before pass as 'result' var: discard "resetLoc(p, d)" pl.add(addrLoc(p.config, d)) genCallPattern() else: var tmp: TLoc getTemp(p, typ[0], tmp, needsInit=true) pl.add(addrLoc(p.config, tmp)) genCallPattern() genAssignment(p, d, tmp, {}) # no need for deep copying else: if d.k == locNone: getTemp(p, typ[0], d) assert(d.t != nil) # generate an assignment to d: var list: TLoc initLoc(list, locCall, d.lode, OnUnknown) if tfIterator in typ.flags: list.r = PatIter % [rdLoc(op), pl, pl.addComma, rawProc] else: list.r = PatProc % [rdLoc(op), pl, pl.addComma, rawProc] genAssignment(p, d, list, {}) # no need for deep copying else: genCallPattern() proc genOtherArg(p: BProc; ri: PNode; i: int; typ: PType): Rope = if i < typ.len: # 'var T' is 'T&' in C++. This means we ignore the request of # any nkHiddenAddr when it's a 'var T'. let paramType = typ.n[i] assert(paramType.kind == nkSym) if paramType.typ.isCompileTimeOnly: result = nil elif typ[i].kind == tyVar and ri[i].kind == nkHiddenAddr: result = genArgNoParam(p, ri[i][0]) else: result = genArgNoParam(p, ri[i]) #, typ.n[i].sym) else: if tfVarargs notin typ.flags: localError(p.config, ri.info, "wrong argument count") result = nil else: result = genArgNoParam(p, ri[i]) discard """ Dot call syntax in C++ ====================== so c2nim translates 'this' sometimes to 'T' and sometimes to 'var T' both of which are wrong, but often more convenient to use. For manual wrappers it can also be 'ptr T' Fortunately we know which parameter is the 'this' parameter and so can fix this mess in the codegen. now ... if the *argument* is a 'ptr' the codegen shall emit -> and otherwise . but this only depends on the argument and not on how the 'this' was declared however how the 'this' was declared affects whether we end up with wrong 'addr' and '[]' ops... Since I'm tired I'll enumerate all the cases here: var x: ptr T y: T proc t(x: T) x[].t() --> (*x).t() is correct. y.t() --> y.t() is correct proc u(x: ptr T) x.u() --> needs to become x->u() (addr y).u() --> needs to become y.u() proc v(x: var T) --> first skip the implicit 'nkAddr' node x[].v() --> (*x).v() is correct, but might have been eliminated due to the nkAddr node! So for this case we need to generate '->' y.v() --> y.v() is correct """ proc skipAddrDeref(node: PNode): PNode = var n = node var isAddr = false case n.kind of nkAddr, nkHiddenAddr: n = n[0] isAddr = true of nkDerefExpr, nkHiddenDeref: n = n[0] else: return n if n.kind == nkObjDownConv: n = n[0] if isAddr and n.kind in {nkDerefExpr, nkHiddenDeref}: result = n[0] elif n.kind in {nkAddr, nkHiddenAddr}: result = n[0] else: result = node proc genThisArg(p: BProc; ri: PNode; i: int; typ: PType): Rope = # for better or worse c2nim translates the 'this' argument to a 'var T'. # However manual wrappers may also use 'ptr T'. In any case we support both # for convenience. internalAssert p.config, i < typ.len assert(typ.n[i].kind == nkSym) # if the parameter is lying (tyVar) and thus we required an additional deref, # skip the deref: var ri = ri[i] while ri.kind == nkObjDownConv: ri = ri[0] let t = typ[i].skipTypes({tyGenericInst, tyAlias, tySink}) if t.kind == tyVar: let x = if ri.kind == nkHiddenAddr: ri[0] else: ri if x.typ.kind == tyPtr: result = genArgNoParam(p, x) result.add("->") elif x.kind in {nkHiddenDeref, nkDerefExpr} and x[0].typ.kind == tyPtr: result = genArgNoParam(p, x[0]) result.add("->") else: result = genArgNoParam(p, x) result.add(".") elif t.kind == tyPtr: if ri.kind in {nkAddr, nkHiddenAddr}: result = genArgNoParam(p, ri[0]) result.add(".") else: result = genArgNoParam(p, ri) result.add("->") else: ri = skipAddrDeref(ri) if ri.kind in {nkAddr, nkHiddenAddr}: ri = ri[0] result = genArgNoParam(p, ri) #, typ.n[i].sym) result.add(".") proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType): Rope = var i = 0 var j = 1 while i < pat.len: case pat[i] of '@': var first = true for k in j.. 0: if not first: result.add(~", ") first = false result.add arg inc i of '#': if i+1 < pat.len and pat[i+1] in {'+', '@'}: let ri = ri[j] if ri.kind in nkCallKinds: let typ = skipTypes(ri[0].typ, abstractInst) if pat[i+1] == '+': result.add genArgNoParam(p, ri[0]) result.add(~"(") if 1 < ri.len: result.add genOtherArg(p, ri, 1, typ) for k in j+1..= start: result.add(substr(pat, start, i - 1)) proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) = var op: TLoc initLocExpr(p, ri[0], op) # getUniqueType() is too expensive here: var typ = skipTypes(ri[0].typ, abstractInst) assert(typ.kind == tyProc) assert(typ.len == typ.n.len) # don't call '$' here for efficiency: let pat = ri[0].sym.loc.r.data internalAssert p.config, pat.len > 0 if pat.contains({'#', '(', '@', '\''}): var pl = genPatternCall(p, ri, pat, typ) # simpler version of 'fixupCall' that works with the pl+params combination: var typ = skipTypes(ri[0].typ, abstractInst) if typ[0] != nil: if p.module.compileToCpp and lfSingleUse in d.flags: # do not generate spurious temporaries for C++! For C we're better off # with them to prevent undefined behaviour and because the codegen # is free to emit expressions multiple times! d.k = locCall d.r = pl excl d.flags, lfSingleUse else: if d.k == locNone: getTemp(p, typ[0], d) assert(d.t != nil) # generate an assignment to d: var list: TLoc initLoc(list, locCall, d.lode, OnUnknown) list.r = pl genAssignment(p, d, list, {}) # no need for deep copying else: pl.add(~";$n") line(p, cpsStmts, pl) else: var pl: Rope = nil #var param = typ.n[1].sym if 1 < ri.len: pl.add(genThisArg(p, ri, 1, typ)) pl.add(op.r) var params: Rope for i in 2.. 0 var start = 3 if ' ' in pat: start = 1 pl.add(op.r) if ri.len > 1: pl.add(~": ") pl.add(genArg(p, ri[1], typ.n[1].sym, ri)) start = 2 else: if ri.len > 1: pl.add(genArg(p, ri[1], typ.n[1].sym, ri)) pl.add(~" ") pl.add(op.r) if ri.len > 2: pl.add(~": ") pl.add(genArg(p, ri[2], typ.n[2].sym, ri)) for i in start..= typ.len: internalError(p.config, ri.info, "varargs for objective C method?") assert(typ.n[i].kind == nkSym) var param = typ.n[i].sym pl.add(~" ") pl.add(param.name.s) pl.add(~": ") pl.add(genArg(p, ri[i], param, ri)) if typ[0] != nil: if isInvalidReturnType(p.config, typ[0]): if ri.len > 1: pl.add(~" ") # beware of 'result = p(result)'. We always allocate a temporary: if d.k in {locTemp, locNone}: # We already got a temp. Great, special case it: if d.k == locNone: getTemp(p, typ[0], d, needsInit=true) pl.add(~"Result: ") pl.add(addrLoc(p.config, d)) pl.add(~"];$n") line(p, cpsStmts, pl) else: var tmp: TLoc getTemp(p, typ[0], tmp, needsInit=true) pl.add(addrLoc(p.config, tmp)) pl.add(~"];$n") line(p, cpsStmts, pl) genAssignment(p, d, tmp, {}) # no need for deep copying else: pl.add(~"]") if d.k == locNone: getTemp(p, typ[0], d) assert(d.t != nil) # generate an assignment to d: var list: TLoc initLoc(list, locCall, ri, OnUnknown) list.r = pl genAssignment(p, d, list, {}) # no need for deep copying else: pl.add(~"];$n") line(p, cpsStmts, pl) proc genCall(p: BProc, e: PNode, d: var TLoc) = if e[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: genClosureCall(p, nil, e, d) elif e[0].kind == nkSym and sfInfixCall in e[0].sym.flags: genInfixCall(p, nil, e, d) elif e[0].kind == nkSym and sfNamedParamCall in e[0].sym.flags: genNamedParamCall(p, e, d) else: genPrefixCall(p, nil, e, d) postStmtActions(p) if p.config.exc == excGoto and canRaise(e[0]): raiseExit(p) proc genAsgnCall(p: BProc, le, ri: PNode, d: var TLoc) = if ri[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: genClosureCall(p, le, ri, d) elif ri[0].kind == nkSym and sfInfixCall in ri[0].sym.flags: genInfixCall(p, le, ri, d) elif ri[0].kind == nkSym and sfNamedParamCall in ri[0].sym.flags: genNamedParamCall(p, ri, d) else: genPrefixCall(p, le, ri, d) postStmtActions(p) if p.config.exc == excGoto and canRaise(ri[0]): raiseExit(p)