diff --git a/compiler/aliases.nim b/compiler/aliases.nim index fb2a39bc5..34b81cbad 100644 --- a/compiler/aliases.nim +++ b/compiler/aliases.nim @@ -22,17 +22,17 @@ proc isPartOfAux(n: PNode, b: PType, marker: var IntSet): TAnalysisResult = result = arNo case n.kind of nkRecList: - for i in 0 ..< len(n): - result = isPartOfAux(n.sons[i], b, marker) + for i in 0..= 2 and len(b) >= 2: + if a.len >= 2 and b.len >= 2: # array accesses: if result == arYes and isDeepConstExpr(a[1]) and isDeepConstExpr(b[1]): # we know it's the same array and we have 2 constant indexes; @@ -181,14 +181,14 @@ proc isPartOf*(a, b: PNode): TAnalysisResult = else: discard of nkObjConstr: result = arNo - for i in 1 ..< b.len: + for i in 1..= nkNone and n.kind <= nkNilLit @@ -1740,7 +1733,7 @@ proc makeStmtList*(n: PNode): PNode = proc skipStmtList*(n: PNode): PNode = if n.kind in {nkStmtList, nkStmtListExpr}: - for i in 0 .. n.len-2: + for i in 0..' suffix that we need to ## ignore, see compiler / evaltempl.nim, snippet: ## - ## .. code-block:: nim + ##..code-block:: nim ## ## result.add newIdentNode(getIdent(c.ic, x.name.s & "`gensym" & $x.id), ## if c.instLines: actual.info else: templ.info) - for i in 1 ..< len(list): + for i in 1.. 0: - addf(result, "$N$1\"flags\": $2", [istr, flagsToStr(n.flags)]) - addf(result, "$N$1\"magic\": $2", [istr, makeYamlString($n.magic)]) - addf(result, "$N$1\"ast\": $2", [istr, ast]) - addf(result, "$N$1\"options\": $2", [istr, flagsToStr(n.options)]) - addf(result, "$N$1\"position\": $2", [istr, rope(n.position)]) - addf(result, "$N$1\"k\": $2", [istr, makeYamlString($n.loc.k)]) - addf(result, "$N$1\"storage\": $2", [istr, makeYamlString($n.loc.storage)]) + result.addf("$N$1\"flags\": $2", [istr, flagsToStr(n.flags)]) + result.addf("$N$1\"magic\": $2", [istr, makeYamlString($n.magic)]) + result.addf("$N$1\"ast\": $2", [istr, ast]) + result.addf("$N$1\"options\": $2", [istr, flagsToStr(n.options)]) + result.addf("$N$1\"position\": $2", [istr, rope(n.position)]) + result.addf("$N$1\"k\": $2", [istr, makeYamlString($n.loc.k)]) + result.addf("$N$1\"storage\": $2", [istr, makeYamlString($n.loc.storage)]) if card(n.loc.flags) > 0: - addf(result, "$N$1\"flags\": $2", [istr, makeYamlString($n.loc.flags)]) - addf(result, "$N$1\"r\": $2", [istr, n.loc.r]) - addf(result, "$N$1\"lode\": $2", [istr, treeToYamlAux(conf, n.loc.lode, marker, indent + 2, maxRecDepth - 1)]) - addf(result, "$N$1}", [rspaces(indent)]) + result.addf("$N$1\"flags\": $2", [istr, makeYamlString($n.loc.flags)]) + result.addf("$N$1\"r\": $2", [istr, n.loc.r]) + result.addf("$N$1\"lode\": $2", [istr, treeToYamlAux(conf, n.loc.lode, marker, indent + 2, maxRecDepth - 1)]) + result.addf("$N$1}", [rspaces(indent)]) proc typeToYamlAux(conf: ConfigRef; n: PType, marker: var IntSet, indent: int, maxRecDepth: int): Rope = @@ -327,27 +327,27 @@ proc typeToYamlAux(conf: ConfigRef; n: PType, marker: var IntSet, indent: int, sonsRope = "\"$1 @$2\"" % [rope($n.kind), rope( strutils.toHex(cast[ByteAddress](n), sizeof(n) * 2))] else: - if len(n) > 0: + if n.len > 0: sonsRope = rope("[") - for i in 0 ..< len(n): - if i > 0: add(sonsRope, ",") - addf(sonsRope, "$N$1$2", [rspaces(indent + 4), typeToYamlAux(conf, n.sons[i], + for i in 0.. 0: sonsRope.add(",") + sonsRope.addf("$N$1$2", [rspaces(indent + 4), typeToYamlAux(conf, n[i], marker, indent + 4, maxRecDepth - 1)]) - addf(sonsRope, "$N$1]", [rspaces(indent + 2)]) + sonsRope.addf("$N$1]", [rspaces(indent + 2)]) else: sonsRope = rope("null") let istr = rspaces(indent + 2) result = rope("{") - addf(result, "$N$1\"kind\": $2", [istr, makeYamlString($n.kind)]) - addf(result, "$N$1\"sym\": $2", [istr, symToYamlAux(conf, n.sym, marker, indent + 2, maxRecDepth - 1)]) - addf(result, "$N$1\"n\": $2", [istr, treeToYamlAux(conf, n.n, marker, indent + 2, maxRecDepth - 1)]) + result.addf("$N$1\"kind\": $2", [istr, makeYamlString($n.kind)]) + result.addf("$N$1\"sym\": $2", [istr, symToYamlAux(conf, n.sym, marker, indent + 2, maxRecDepth - 1)]) + result.addf("$N$1\"n\": $2", [istr, treeToYamlAux(conf, n.n, marker, indent + 2, maxRecDepth - 1)]) if card(n.flags) > 0: - addf(result, "$N$1\"flags\": $2", [istr, flagsToStr(n.flags)]) - addf(result, "$N$1\"callconv\": $2", [istr, makeYamlString(CallingConvToStr[n.callConv])]) - addf(result, "$N$1\"size\": $2", [istr, rope(n.size)]) - addf(result, "$N$1\"align\": $2", [istr, rope(n.align)]) - addf(result, "$N$1\"sons\": $2", [istr, sonsRope]) + result.addf("$N$1\"flags\": $2", [istr, flagsToStr(n.flags)]) + result.addf("$N$1\"callconv\": $2", [istr, makeYamlString(CallingConvToStr[n.callConv])]) + result.addf("$N$1\"size\": $2", [istr, rope(n.size)]) + result.addf("$N$1\"align\": $2", [istr, rope(n.align)]) + result.addf("$N$1\"sons\": $2", [istr, sonsRope]) proc treeToYamlAux(conf: ConfigRef; n: PNode, marker: var IntSet, indent: int, maxRecDepth: int): Rope = @@ -358,34 +358,34 @@ proc treeToYamlAux(conf: ConfigRef; n: PNode, marker: var IntSet, indent: int, result = "{$N$1\"kind\": $2" % [istr, makeYamlString($n.kind)] if maxRecDepth != 0: if conf != nil: - addf(result, ",$N$1\"info\": $2", [istr, lineInfoToStr(conf, n.info)]) + result.addf(",$N$1\"info\": $2", [istr, lineInfoToStr(conf, n.info)]) case n.kind of nkCharLit..nkInt64Lit: - addf(result, ",$N$1\"intVal\": $2", [istr, rope(n.intVal)]) + result.addf(",$N$1\"intVal\": $2", [istr, rope(n.intVal)]) of nkFloatLit, nkFloat32Lit, nkFloat64Lit: - addf(result, ",$N$1\"floatVal\": $2", + result.addf(",$N$1\"floatVal\": $2", [istr, rope(n.floatVal.toStrMaxPrecision)]) of nkStrLit..nkTripleStrLit: - addf(result, ",$N$1\"strVal\": $2", [istr, makeYamlString(n.strVal)]) + result.addf(",$N$1\"strVal\": $2", [istr, makeYamlString(n.strVal)]) of nkSym: - addf(result, ",$N$1\"sym\": $2", + result.addf(",$N$1\"sym\": $2", [istr, symToYamlAux(conf, n.sym, marker, indent + 2, maxRecDepth)]) of nkIdent: if n.ident != nil: - addf(result, ",$N$1\"ident\": $2", [istr, makeYamlString(n.ident.s)]) + result.addf(",$N$1\"ident\": $2", [istr, makeYamlString(n.ident.s)]) else: - addf(result, ",$N$1\"ident\": null", [istr]) + result.addf(",$N$1\"ident\": null", [istr]) else: - if len(n) > 0: - addf(result, ",$N$1\"sons\": [", [istr]) - for i in 0 ..< len(n): - if i > 0: add(result, ",") - addf(result, "$N$1$2", [rspaces(indent + 4), treeToYamlAux(conf, n.sons[i], + if n.len > 0: + result.addf(",$N$1\"sons\": [", [istr]) + for i in 0.. 0: result.add(",") + result.addf("$N$1$2", [rspaces(indent + 4), treeToYamlAux(conf, n[i], marker, indent + 4, maxRecDepth - 1)]) - addf(result, "$N$1]", [istr]) - addf(result, ",$N$1\"typ\": $2", + result.addf("$N$1]", [istr]) + result.addf(",$N$1\"typ\": $2", [istr, typeToYamlAux(conf, n.typ, marker, indent + 2, maxRecDepth)]) - addf(result, "$N$1}", [rspaces(indent)]) + result.addf("$N$1}", [rspaces(indent)]) proc treeToYaml(conf: ConfigRef; n: PNode, indent: int = 0, maxRecDepth: int = - 1): Rope = var marker = initIntSet() @@ -427,7 +427,7 @@ proc indentLess(this: var DebugPrinter) = proc newlineAndIndent(this: var DebugPrinter) = this.res.add "\n" this.currentLine += 1 - for i in 0 ..< this.indent: + for i in 0.. 0: + if value.len > 0: this.key "sons" this.openBracket - for i in 0 ..< len(value): - this.value value.sons[i] - if i != len(value) - 1: + for i in 0.. 0: + if value.len > 0: this.key "sons" this.openBracket - for i in 0 ..< len(value): - this.value value.sons[i] - if i != len(value) - 1: + for i in 0..') + result.add('>') proc encodeType(w: PRodWriter, t: PType, result: var string) = if t == nil: @@ -277,47 +277,47 @@ proc encodeType(w: PRodWriter, t: PType, result: var string) = if t.kind == tyForward: internalError("encodeType: tyForward") # for the new rodfile viewer we use a preceding [ so that the data section # can easily be disambiguated: - add(result, '[') + result.add('[') encodeVInt(ord(t.kind), result) - add(result, '+') + result.add('+') encodeVInt(t.id, result) if t.n != nil: encodeNode(w, unknownLineInfo(), t.n, result) if t.flags != {}: - add(result, '$') + result.add('$') encodeVInt(cast[int32](t.flags), result) if t.callConv != low(t.callConv): - add(result, '?') + result.add('?') encodeVInt(ord(t.callConv), result) if t.owner != nil: - add(result, '*') + result.add('*') encodeVInt(t.owner.id, result) pushSym(w, t.owner) if t.sym != nil: - add(result, '&') + result.add('&') encodeVInt(t.sym.id, result) pushSym(w, t.sym) if t.size != - 1: - add(result, '/') + result.add('/') encodeVBiggestInt(t.size, result) if t.align != - 1: - add(result, '=') + result.add('=') encodeVInt(t.align, result) encodeLoc(w, t.loc, result) - for i in 0 ..< len(t): - if t.sons[i] == nil: - add(result, "^()") + for i in 0.. 0: - for i in 0..q.len-2: + for i in 0.. 1: add(pl, ~", ") + if typ[0] != nil: + if isInvalidReturnType(p.config, typ[0]): + if ri.len > 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.sons[0], d, needsInit=true) + 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)" - add(pl, addrLoc(p.config, d)) + pl.add(addrLoc(p.config, d)) genCallPattern() else: var tmp: TLoc - getTemp(p, typ.sons[0], tmp, needsInit=true) - add(pl, addrLoc(p.config, tmp)) + 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.sons[0], d) + 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) @@ -280,23 +278,23 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) = genCallPattern() proc genOtherArg(p: BProc; ri: PNode; i: int; typ: PType): Rope = - if i < len(typ): + 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.sons[i] + let paramType = typ.n[i] assert(paramType.kind == nkSym) if paramType.typ.isCompileTimeOnly: result = nil - elif typ.sons[i].kind == tyVar and ri.sons[i].kind == nkHiddenAddr: - result = genArgNoParam(p, ri.sons[i][0]) + elif typ[i].kind == tyVar and ri[i].kind == nkHiddenAddr: + result = genArgNoParam(p, ri[i][0]) else: - result = genArgNoParam(p, ri.sons[i]) #, typ.n.sons[i].sym) + 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.sons[i]) + result = genArgNoParam(p, ri[i]) discard """ Dot call syntax in C++ @@ -340,16 +338,16 @@ proc skipAddrDeref(node: PNode): PNode = var isAddr = false case n.kind of nkAddr, nkHiddenAddr: - n = n.sons[0] + n = n[0] isAddr = true of nkDerefExpr, nkHiddenDeref: - n = n.sons[0] + n = n[0] else: return n - if n.kind == nkObjDownConv: n = n.sons[0] + if n.kind == nkObjDownConv: n = n[0] if isAddr and n.kind in {nkDerefExpr, nkHiddenDeref}: - result = n.sons[0] + result = n[0] elif n.kind in {nkAddr, nkHiddenAddr}: - result = n.sons[0] + result = n[0] else: result = node @@ -357,13 +355,13 @@ 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 < len(typ) - assert(typ.n.sons[i].kind == nkSym) + 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.sons[i].skipTypes({tyGenericInst, tyAlias, tySink}) + 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: @@ -385,7 +383,7 @@ proc genThisArg(p: BProc; ri: PNode; i: int; typ: PType): Rope = else: ri = skipAddrDeref(ri) if ri.kind in {nkAddr, nkHiddenAddr}: ri = ri[0] - result = genArgNoParam(p, ri) #, typ.n.sons[i].sym) + result = genArgNoParam(p, ri) #, typ.n[i].sym) result.add(".") proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType): Rope = @@ -395,7 +393,7 @@ proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType): Rope = case pat[i] of '@': var first = true - for k in j ..< ri.len: + for k in j.. 0: if not first: @@ -407,12 +405,12 @@ proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType): Rope = if i+1 < pat.len and pat[i+1] in {'+', '@'}: let ri = ri[j] if ri.kind in nkCallKinds: - let typ = skipTypes(ri.sons[0].typ, abstractInst) - if pat[i+1] == '+': result.add genArgNoParam(p, ri.sons[0]) + 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 ..< ri.len: + for k in j+1..= start: - add(result, substr(pat, start, i - 1)) + result.add(substr(pat, start, i - 1)) proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) = var op: TLoc - initLocExpr(p, ri.sons[0], op) + initLocExpr(p, ri[0], op) # getUniqueType() is too expensive here: - var typ = skipTypes(ri.sons[0].typ, abstractInst) + var typ = skipTypes(ri[0].typ, abstractInst) assert(typ.kind == tyProc) - var length = len(ri) - assert(len(typ) == len(typ.n)) + assert(typ.len == typ.n.len) # don't call '$' here for efficiency: - let pat = ri.sons[0].sym.loc.r.data + 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.sons[0].typ, abstractInst) - if typ.sons[0] != nil: + 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 @@ -469,115 +466,114 @@ proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) = d.r = pl excl d.flags, lfSingleUse else: - if d.k == locNone: getTemp(p, typ.sons[0], d) + 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: - add(pl, ~";$n") + pl.add(~";$n") line(p, cpsStmts, pl) else: var pl: Rope = nil - #var param = typ.n.sons[1].sym + #var param = typ.n[1].sym if 1 < ri.len: - add(pl, genThisArg(p, ri, 1, typ)) - add(pl, op.r) + pl.add(genThisArg(p, ri, 1, typ)) + pl.add(op.r) var params: Rope - for i in 2 ..< length: + for i in 2.. 0 var start = 3 if ' ' in pat: start = 1 - add(pl, op.r) - if length > 1: - add(pl, ~": ") - add(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym, ri)) + 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 length > 1: - add(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym, ri)) - add(pl, ~" ") - add(pl, op.r) - if length > 2: - add(pl, ~": ") - add(pl, genArg(p, ri.sons[2], typ.n.sons[2].sym, ri)) - for i in start ..< length: - assert(len(typ) == len(typ.n)) - if i >= len(typ): + 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.sons[i].kind == nkSym) - var param = typ.n.sons[i].sym - add(pl, ~" ") - add(pl, param.name.s) - add(pl, ~": ") - add(pl, genArg(p, ri.sons[i], param, ri)) - if typ.sons[0] != nil: - if isInvalidReturnType(p.config, typ.sons[0]): - if len(ri) > 1: add(pl, ~" ") + 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.sons[0], d, needsInit=true) - add(pl, ~"Result: ") - add(pl, addrLoc(p.config, d)) - add(pl, ~"];$n") + 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.sons[0], tmp, needsInit=true) - add(pl, addrLoc(p.config, tmp)) - add(pl, ~"];$n") + 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: - add(pl, ~"]") - if d.k == locNone: getTemp(p, typ.sons[0], d) + 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: - add(pl, ~"];$n") + pl.add(~"];$n") line(p, cpsStmts, pl) proc genCall(p: BProc, e: PNode, d: var TLoc) = - if e.sons[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: + if e[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: genClosureCall(p, nil, e, d) - elif e.sons[0].kind == nkSym and sfInfixCall in e.sons[0].sym.flags: + elif e[0].kind == nkSym and sfInfixCall in e[0].sym.flags: genInfixCall(p, nil, e, d) - elif e.sons[0].kind == nkSym and sfNamedParamCall in e.sons[0].sym.flags: + elif e[0].kind == nkSym and sfNamedParamCall in e[0].sym.flags: genNamedParamCall(p, e, d) else: genPrefixCall(p, nil, e, d) postStmtActions(p) proc genAsgnCall(p: BProc, le, ri: PNode, d: var TLoc) = - if ri.sons[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: + if ri[0].typ.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).callConv == ccClosure: genClosureCall(p, le, ri, d) - elif ri.sons[0].kind == nkSym and sfInfixCall in ri.sons[0].sym.flags: + elif ri[0].kind == nkSym and sfInfixCall in ri[0].sym.flags: genInfixCall(p, le, ri, d) - elif ri.sons[0].kind == nkSym and sfNamedParamCall in ri.sons[0].sym.flags: + elif ri[0].kind == nkSym and sfNamedParamCall in ri[0].sym.flags: genNamedParamCall(p, ri, d) else: genPrefixCall(p, le, ri, d) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 009ee7221..a95086c71 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -64,7 +64,7 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): Rope = if id == p.module.labels: # not found in cache: inc(p.module.labels) - addf(p.module.s[cfsData], + p.module.s[cfsData].addf( "static NIM_CONST $1 $2 = {NIM_NIL,NIM_NIL};$n", [getTypeDesc(p.module, ty), result]) else: @@ -98,13 +98,13 @@ proc genLiteral(p: BProc, n: PNode): Rope = proc bitSetToWord(s: TBitSet, size: int): BiggestUInt = result = 0 - for j in 0 ..< size: - if j < len(s): result = result or (BiggestUInt(s[j]) shl (j * 8)) + for j in 0.. 8: var res = "{\n" - for i in 0 ..< size: + for i in 0.. Losing auto vectorization then? - expr(p, e.sons[0], d) - if e.sons[0].typ.skipTypes(abstractInstOwned).kind == tyRef: + expr(p, e[0], d) + if e[0].typ.skipTypes(abstractInstOwned).kind == tyRef: d.storage = OnHeap else: var a: TLoc - var typ = e.sons[0].typ + var typ = e[0].typ if typ.kind in {tyUserTypeClass, tyUserTypeClassInst} and typ.isResolvedUserTypeClass: typ = typ.lastSon typ = typ.skipTypes(abstractInstOwned) - if typ.kind == tyVar and tfVarIsPtr notin typ.flags and p.module.compileToCpp and e.sons[0].kind == nkHiddenAddr: + if typ.kind == tyVar and tfVarIsPtr notin typ.flags and p.module.compileToCpp and e[0].kind == nkHiddenAddr: initLocExprSingleUse(p, e[0][0], d) return else: - initLocExprSingleUse(p, e.sons[0], a) + initLocExprSingleUse(p, e[0], a) if d.k == locNone: # dest = *a; <-- We do not know that 'dest' is on the heap! # It is completely wrong to set 'd.storage' here, unless it's not yet @@ -716,30 +716,30 @@ proc genDeref(p: BProc, e: PNode, d: var TLoc) = # See tmissingderef. So we get rid of the deref instead. The codegen # ends up using 'memcpy' for the array assignment, # so the '&' and '*' cancel out: - putIntoDest(p, d, lodeTyp(a.t.sons[0]), rdLoc(a), a.storage) + putIntoDest(p, d, lodeTyp(a.t[0]), rdLoc(a), a.storage) else: putIntoDest(p, d, e, "(*$1)" % [rdLoc(a)], a.storage) proc genAddr(p: BProc, e: PNode, d: var TLoc) = # careful 'addr(myptrToArray)' needs to get the ampersand: - if e.sons[0].typ.skipTypes(abstractInstOwned).kind in {tyRef, tyPtr}: + if e[0].typ.skipTypes(abstractInstOwned).kind in {tyRef, tyPtr}: var a: TLoc - initLocExpr(p, e.sons[0], a) + initLocExpr(p, e[0], a) putIntoDest(p, d, e, "&" & a.r, a.storage) #Message(e.info, warnUser, "HERE NEW &") - elif mapType(p.config, e.sons[0].typ) == ctArray or isCppRef(p, e.typ): - expr(p, e.sons[0], d) + elif mapType(p.config, e[0].typ) == ctArray or isCppRef(p, e.typ): + expr(p, e[0], d) else: var a: TLoc - initLocExpr(p, e.sons[0], a) + initLocExpr(p, e[0], a) putIntoDest(p, d, e, addrLoc(p.config, a), a.storage) template inheritLocation(d: var TLoc, a: TLoc) = if d.k == locNone: d.storage = a.storage proc genRecordFieldAux(p: BProc, e: PNode, d, a: var TLoc) = - initLocExpr(p, e.sons[0], a) - if e.sons[1].kind != nkSym: internalError(p.config, e.info, "genRecordFieldAux") + initLocExpr(p, e[0], a) + if e[1].kind != nkSym: internalError(p.config, e.info, "genRecordFieldAux") d.inheritLocation(a) discard getTypeDesc(p.module, a.t) # fill the record's fields.loc @@ -747,16 +747,16 @@ proc genTupleElem(p: BProc, e: PNode, d: var TLoc) = var a: TLoc i: int - initLocExpr(p, e.sons[0], a) + initLocExpr(p, e[0], a) let tupType = a.t.skipTypes(abstractInst+{tyVar}) assert tupType.kind == tyTuple d.inheritLocation(a) discard getTypeDesc(p.module, a.t) # fill the record's fields.loc var r = rdLoc(a) - case e.sons[1].kind - of nkIntLit..nkUInt64Lit: i = int(e.sons[1].intVal) + case e[1].kind + of nkIntLit..nkUInt64Lit: i = int(e[1].intVal) else: internalError(p.config, e.info, "genTupleElem") - addf(r, ".Field$1", [rope(i)]) + r.addf(".Field$1", [rope(i)]) putIntoDest(p, d, e, r, a.storage) proc lookupFieldAgain(p: BProc, ty: PType; field: PSym; r: var Rope; @@ -770,43 +770,43 @@ proc lookupFieldAgain(p: BProc, ty: PType; field: PSym; r: var Rope; if result != nil: if resTyp != nil: resTyp[] = ty break - if not p.module.compileToCpp: add(r, ".Sup") - ty = ty.sons[0] + if not p.module.compileToCpp: r.add(".Sup") + ty = ty[0] if result == nil: internalError(p.config, field.info, "genCheckedRecordField") proc genRecordField(p: BProc, e: PNode, d: var TLoc) = var a: TLoc genRecordFieldAux(p, e, d, a) var r = rdLoc(a) - var f = e.sons[1].sym + var f = e[1].sym let ty = skipTypes(a.t, abstractInstOwned + tyUserTypeClasses) if ty.kind == tyTuple: # we found a unique tuple type which lacks field information # so we use Field$i - addf(r, ".Field$1", [rope(f.position)]) + r.addf(".Field$1", [rope(f.position)]) putIntoDest(p, d, e, r, a.storage) else: var rtyp: PType let field = lookupFieldAgain(p, ty, f, r, addr rtyp) if field.loc.r == nil and rtyp != nil: fillObjectFields(p.module, rtyp) if field.loc.r == nil: internalError(p.config, e.info, "genRecordField 3 " & typeToString(ty)) - addf(r, ".$1", [field.loc.r]) + r.addf(".$1", [field.loc.r]) putIntoDest(p, d, e, r, a.storage) proc genInExprAux(p: BProc, e: PNode, a, b, d: var TLoc) proc genFieldCheck(p: BProc, e: PNode, obj: Rope, field: PSym) = var test, u, v: TLoc - for i in 1 ..< len(e): - var it = e.sons[i] + for i in 1..data, $1->len)", [rdLoc(a)])) + args.add(ropecg(p.module, ", Genode::Cstring($1->data, $1->len)", [rdLoc(a)])) p.module.includeHeader("") p.module.includeHeader("") linefmt(p, cpsStmts, """Genode::log(""$1);$n""", [args]) @@ -1087,21 +1087,21 @@ proc genStrConcat(p: BProc, e: PNode, d: var TLoc) = var L = 0 var appends: Rope = nil var lens: Rope = nil - for i in 0 .. len(e) - 2: + for i in 0.. # seq = (typeof seq) incrSeq(&seq->Sup, sizeof(x)); # seq->data[seq->len-1] = x; var a, b, dest, tmpL, call: TLoc - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) - let seqType = skipTypes(e.sons[1].typ, {tyVar}) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) + let seqType = skipTypes(e[1].typ, {tyVar}) initLoc(call, locCall, e, OnHeap) if not p.module.compileToCpp: const seqAppendPattern = "($2) #incrSeqV3((TGenericSeq*)($1), $3)" call.r = ropecg(p.module, seqAppendPattern, [rdLoc(a), - getTypeDesc(p.module, e.sons[1].typ), + getTypeDesc(p.module, e[1].typ), genTypeInfo(p.module, seqType, e.info)]) else: const seqAppendPattern = "($2) #incrSeqV3($1, $3)" call.r = ropecg(p.module, seqAppendPattern, [rdLoc(a), - getTypeDesc(p.module, e.sons[1].typ), + getTypeDesc(p.module, e[1].typ), genTypeInfo(p.module, seqType, e.info)]) # emit the write barrier if required, but we can always move here, so # use 'genRefAssign' for the seq. genRefAssign(p, a, call) #if bt != b.t: # echo "YES ", e.info, " new: ", typeToString(bt), " old: ", typeToString(b.t) - initLoc(dest, locExpr, e.sons[2], OnHeap) + initLoc(dest, locExpr, e[2], OnHeap) getIntTemp(p, tmpL) lineCg(p, cpsStmts, "$1 = $2->$3++;$n", [tmpL.r, rdLoc(a), lenField(p)]) dest.r = ropecg(p.module, "$1$3[$2]", [rdLoc(a), tmpL.r, dataField(p)]) @@ -1184,7 +1184,7 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) = proc genReset(p: BProc, n: PNode) = var a: TLoc - initLocExpr(p, n.sons[1], a) + initLocExpr(p, n[1], a) linefmt(p, cpsStmts, "#genericReset((void*)$1, $2);$n", [addrLoc(p.config, a), genTypeInfo(p.module, skipTypes(a.t, {tyVar}), n.info)]) @@ -1220,7 +1220,7 @@ proc rawGenNew(p: BProc, a: TLoc, sizeExpr: Rope) = "to have the 'nimcall' calling convention") var f: TLoc initLocExpr(p, newSymNode(bt.destructor), f) - addf(p.module.s[cfsTypeInit3], "$1->finalizer = (void*)$2;$n", [ti, rdLoc(f)]) + p.module.s[cfsTypeInit3].addf("$1->finalizer = (void*)$2;$n", [ti, rdLoc(f)]) if a.storage == OnHeap and usesWriteBarrier(p.config): if canFormAcycle(a.t): @@ -1245,11 +1245,11 @@ proc rawGenNew(p: BProc, a: TLoc, sizeExpr: Rope) = proc genNew(p: BProc, e: PNode) = var a: TLoc - initLocExpr(p, e.sons[1], a) + initLocExpr(p, e[1], a) # 'genNew' also handles 'unsafeNew': if e.len == 3: var se: TLoc - initLocExpr(p, e.sons[2], se) + initLocExpr(p, e[2], se) rawGenNew(p, a, se.rdLoc) else: rawGenNew(p, a, nil) @@ -1284,10 +1284,10 @@ proc genNewSeqAux(p: BProc, dest: TLoc, length: Rope; lenIsZero: bool) = proc genNewSeq(p: BProc, e: PNode) = var a, b: TLoc - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) if optSeqDestructors in p.config.globalOptions: - let seqtype = skipTypes(e.sons[1].typ, abstractVarRange) + let seqtype = skipTypes(e[1].typ, abstractVarRange) linefmt(p, cpsStmts, "$1.len = $2; $1.p = ($4*) #newSeqPayload($2, sizeof($3));$n", [a.rdLoc, b.rdLoc, getTypeDesc(p.module, seqtype.lastSon), getSeqPayloadType(p.module, seqtype)]) @@ -1300,7 +1300,7 @@ proc genNewSeq(p: BProc, e: PNode) = proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) = let seqtype = skipTypes(e.typ, abstractVarRange) var a: TLoc - initLocExpr(p, e.sons[1], a) + initLocExpr(p, e[1], a) if optSeqDestructors in p.config.globalOptions: if d.k == locNone: getTemp(p, e.typ, d, needsInit=false) linefmt(p, cpsStmts, "$1.len = 0; $1.p = ($4*) #newSeqPayload($2, sizeof($3));$n", @@ -1323,7 +1323,7 @@ proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool = if id == p.module.labels: # expression not found in the cache: inc(p.module.labels) - addf(p.module.s[cfsData], "NIM_CONST $1 $2 = $3;$n", + p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n", [getTypeDesc(p.module, t), d.r, genConstExpr(p, n)]) result = true else: @@ -1361,25 +1361,25 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) = r = rdLoc(d) discard getTypeDesc(p.module, t) let ty = getUniqueType(t) - for i in 1 ..< e.len: - let it = e.sons[i] + for i in 1..finalizer = (void*)$2;$n", [ti, rdLoc(f)]) + p.module.s[cfsTypeInit3].addf("$1->finalizer = (void*)$2;$n", [ti, rdLoc(f)]) b.r = ropecg(p.module, "($1) #newObj($2, sizeof($3))", [ getTypeDesc(p.module, refType), ti, getTypeDesc(p.module, skipTypes(refType.lastSon, abstractRange))]) @@ -1496,7 +1496,7 @@ proc genOfHelper(p: BProc; dest: PType; a: Rope; info: TLineInfo): Rope = discard cgsym(p.module, "TNimType") inc p.module.labels let cache = "Nim_OfCheck_CACHE" & p.module.labels.rope - addf(p.module.s[cfsVars], "static TNimType* $#[2];$n", [cache]) + p.module.s[cfsVars].addf("static TNimType* $#[2];$n", [cache]) result = ropecg(p.module, "#isObjWithCache($#.m_type, $#, $#)", [a, ti, cache]) when false: # former version: @@ -1517,9 +1517,9 @@ proc genOf(p: BProc, x: PNode, typ: PType, d: var TLoc) = t = skipTypes(t.lastSon, typedescInst+{tyOwned}) discard getTypeDesc(p.module, t) if not p.module.compileToCpp: - while t.kind == tyObject and t.sons[0] != nil: - add(r, ~".Sup") - t = skipTypes(t.sons[0], skipPtrs) + while t.kind == tyObject and t[0] != nil: + r.add(~".Sup") + t = skipTypes(t[0], skipPtrs) if isObjLackingTypeField(t): globalError(p.config, x.info, "no 'of' operator available for pure objects") @@ -1530,14 +1530,14 @@ proc genOf(p: BProc, x: PNode, typ: PType, d: var TLoc) = putIntoDest(p, d, x, r, a.storage) proc genOf(p: BProc, n: PNode, d: var TLoc) = - genOf(p, n.sons[1], n.sons[2].typ, d) + genOf(p, n[1], n[2].typ, d) proc genRepr(p: BProc, e: PNode, d: var TLoc) = if optTinyRtti in p.config.globalOptions: localError(p.config, e.info, "'repr' is not available for --newruntime") var a: TLoc - initLocExpr(p, e.sons[1], a) - var t = skipTypes(e.sons[1].typ, abstractVarRange) + initLocExpr(p, e[1], a) + var t = skipTypes(e[1].typ, abstractVarRange) case t.kind of tyInt..tyInt64, tyUInt..tyUInt64: putIntoDest(p, d, e, @@ -1568,7 +1568,7 @@ proc genRepr(p: BProc, e: PNode, d: var TLoc) = of tyArray: putIntoDest(p, b, e, "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, a.t))], a.storage) - else: internalError(p.config, e.sons[0].info, "genRepr()") + else: internalError(p.config, e[0].info, "genRepr()") putIntoDest(p, d, e, ropecg(p.module, "#reprOpenArray($1, $2)", [rdLoc(b), genTypeInfo(p.module, elemType(t), e.info)]), a.storage) @@ -1585,12 +1585,12 @@ proc genRepr(p: BProc, e: PNode, d: var TLoc) = gcUsage(p.config, e) proc genGetTypeInfo(p: BProc, e: PNode, d: var TLoc) = - let t = e.sons[1].typ + let t = e[1].typ putIntoDest(p, d, e, genTypeInfo(p.module, t, e.info)) template genDollar(p: BProc, n: PNode, d: var TLoc, frmt: string) = var a: TLoc - initLocExpr(p, n.sons[1], a) + initLocExpr(p, n[1], a) a.r = ropecg(p.module, frmt, [rdLoc(a)]) a.flags = a.flags - {lfIndirect} # this flag should not be propagated here (not just for HCR) if d.k == locNone: getTemp(p, n.typ, d) @@ -1598,8 +1598,8 @@ template genDollar(p: BProc, n: PNode, d: var TLoc, frmt: string) = gcUsage(p.config, n) proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) = - var a = e.sons[1] - if a.kind == nkHiddenAddr: a = a.sons[0] + var a = e[1] + if a.kind == nkHiddenAddr: a = a[0] var typ = skipTypes(a.typ, abstractVar + tyUserTypeClasses) case typ.kind of tyOpenArray, tyVarargs: @@ -1621,7 +1621,7 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) = else: unaryExpr(p, e, d, "($1 ? #nimCStrLen($1) : 0)") of tyString: var a: TLoc - initLocExpr(p, e.sons[1], a) + initLocExpr(p, e[1], a) var x = lenExpr(p, a) if op == mHigh: x = "($1-1)" % [x] putIntoDest(p, d, e, x) @@ -1653,16 +1653,16 @@ proc makeAddr(n: PNode): PNode = proc genSetLengthSeq(p: BProc, e: PNode, d: var TLoc) = if optSeqDestructors in p.config.globalOptions: - e.sons[1] = makeAddr(e[1]) + e[1] = makeAddr(e[1]) genCall(p, e, d) return var a, b, call: TLoc assert(d.k == locNone) - var x = e.sons[1] + var x = e[1] if x.kind in {nkAddr, nkHiddenAddr}: x = x[0] initLocExpr(p, x, a) - initLocExpr(p, e.sons[2], b) - let t = skipTypes(e.sons[1].typ, {tyVar}) + initLocExpr(p, e[2], b) + let t = skipTypes(e[1].typ, {tyVar}) initLoc(call, locCall, e, OnHeap) if not p.module.compileToCpp: @@ -1686,8 +1686,8 @@ proc genSetLengthStr(p: BProc, e: PNode, d: var TLoc) = else: var a, b, call: TLoc if d.k != locNone: internalError(p.config, e.info, "genSetLengthStr") - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) initLoc(call, locCall, e, OnHeap) call.r = ropecg(p.module, "#setLengthStr($1, $2)", [ @@ -1701,9 +1701,9 @@ proc genSwap(p: BProc, e: PNode, d: var TLoc) = # a = b # b = temp var a, b, tmp: TLoc - getTemp(p, skipTypes(e.sons[1].typ, abstractVar), tmp) - initLocExpr(p, e.sons[1], a) # eval a - initLocExpr(p, e.sons[2], b) # eval b + getTemp(p, skipTypes(e[1].typ, abstractVar), tmp) + initLocExpr(p, e[1], a) # eval a + initLocExpr(p, e[2], b) # eval b genAssignment(p, tmp, a, {}) genAssignment(p, a, b, {}) genAssignment(p, b, tmp, {}) @@ -1726,13 +1726,13 @@ proc fewCmps(conf: ConfigRef; s: PNode): bool = elif elemType(s.typ).kind in {tyInt, tyInt16..tyInt64}: result = true # better not emit the set if int is basetype! else: - result = len(s) <= 8 # 8 seems to be a good value + result = s.len <= 8 # 8 seems to be a good value template binaryExprIn(p: BProc, e: PNode, a, b, d: var TLoc, frmt: string) = putIntoDest(p, d, e, frmt % [rdLoc(a), rdSetElemLoc(p.config, b, a.t)]) proc genInExprAux(p: BProc, e: PNode, a, b, d: var TLoc) = - case int(getSize(p.config, skipTypes(e.sons[1].typ, abstractVar))) + case int(getSize(p.config, skipTypes(e[1].typ, abstractVar))) of 1: binaryExprIn(p, e, a, b, d, "(($1 &(1U<<((NU)($2)&7U)))!=0)") of 2: binaryExprIn(p, e, a, b, d, "(($1 &(1U<<((NU)($2)&15U)))!=0)") of 4: binaryExprIn(p, e, a, b, d, "(($1 &(1U<<((NU)($2)&31U)))!=0)") @@ -1742,47 +1742,46 @@ proc genInExprAux(p: BProc, e: PNode, a, b, d: var TLoc) = template binaryStmtInExcl(p: BProc, e: PNode, d: var TLoc, frmt: string) = var a, b: TLoc assert(d.k == locNone) - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) lineF(p, cpsStmts, frmt, [rdLoc(a), rdSetElemLoc(p.config, b, a.t)]) proc genInOp(p: BProc, e: PNode, d: var TLoc) = var a, b, x, y: TLoc - if (e.sons[1].kind == nkCurly) and fewCmps(p.config, e.sons[1]): + if (e[1].kind == nkCurly) and fewCmps(p.config, e[1]): # a set constructor but not a constant set: # do not emit the set, but generate a bunch of comparisons; and if we do # so, we skip the unnecessary range check: This is a semantical extension # that code now relies on. :-/ XXX - let ea = if e.sons[2].kind in {nkChckRange, nkChckRange64}: - e.sons[2].sons[0] + let ea = if e[2].kind in {nkChckRange, nkChckRange64}: + e[2][0] else: - e.sons[2] + e[2] initLocExpr(p, ea, a) initLoc(b, locExpr, e, OnUnknown) - var length = len(e.sons[1]) - if length > 0: + if e[1].len > 0: b.r = rope("(") - for i in 0 ..< length: - let it = e.sons[1].sons[i] + for i in 0..= $2 && $1 <= $3", + initLocExpr(p, it[0], x) + initLocExpr(p, it[1], y) + b.r.addf("$1 >= $2 && $1 <= $3", [rdCharLoc(a), rdCharLoc(x), rdCharLoc(y)]) else: initLocExpr(p, it, x) - addf(b.r, "$1 == $2", [rdCharLoc(a), rdCharLoc(x)]) - if i < length - 1: add(b.r, " || ") - add(b.r, ")") + b.r.addf("$1 == $2", [rdCharLoc(a), rdCharLoc(x)]) + if i < e[1].len - 1: b.r.add(" || ") + b.r.add(")") else: # handle the case of an empty set b.r = rope("0") putIntoDest(p, d, e, b.r) else: - assert(e.sons[1].typ != nil) - assert(e.sons[2].typ != nil) - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + assert(e[1].typ != nil) + assert(e[2].typ != nil) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) genInExprAux(p, e, a, b, d) proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) = @@ -1800,7 +1799,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) = "& ~", "^"] var a, b, i: TLoc - var setType = skipTypes(e.sons[1].typ, abstractVar) + var setType = skipTypes(e[1].typ, abstractVar) var size = int(getSize(p.config, setType)) case size of 1, 2, 4, 8: @@ -1838,12 +1837,12 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mExcl: binaryStmtInExcl(p, e, d, "$1[(NU)($2)>>3] &= ~(1U<<($2&7U));$n") of mCard: var a: TLoc - initLocExpr(p, e.sons[1], a) + initLocExpr(p, e[1], a) putIntoDest(p, d, e, ropecg(p.module, "#cardSet($1, $2)", [rdCharLoc(a), size])) of mLtSet, mLeSet: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), i) # our counter - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) if d.k == locNone: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyBool), d) if op == mLtSet: linefmt(p, cpsStmts, lookupOpr[mLtSet], @@ -1853,16 +1852,16 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) = [rdLoc(i), size, rdLoc(d), rdLoc(a), rdLoc(b)]) of mEqSet: var a, b: TLoc - assert(e.sons[1].typ != nil) - assert(e.sons[2].typ != nil) - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + assert(e[1].typ != nil) + assert(e[2].typ != nil) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) putIntoDest(p, d, e, ropecg(p.module, "(#nimCmpMem($1, $2, $3)==0)", [a.rdCharLoc, b.rdCharLoc, size])) of mMulSet, mPlusSet, mMinusSet, mSymDiffSet: # we inline the simple for loop for better code generation: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), i) # our counter - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) if d.k == locNone: getTemp(p, setType, d) lineF(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) $n" & @@ -1881,9 +1880,9 @@ proc genSomeCast(p: BProc, e: PNode, d: var TLoc) = # we use whatever C gives us. Except if we have a value-type, we need to go # through its address: var a: TLoc - initLocExpr(p, e.sons[1], a) + initLocExpr(p, e[1], a) let etyp = skipTypes(e.typ, abstractRange+{tyOwned}) - let srcTyp = skipTypes(e.sons[1].typ, abstractRange) + let srcTyp = skipTypes(e[1].typ, abstractRange) if etyp.kind in ValueTypes and lfIndirect notin a.flags: putIntoDest(p, d, e, "(*($1*) ($2))" % [getTypeDesc(p.module, e.typ), addrLoc(p.config, a)], a.storage) @@ -1906,7 +1905,7 @@ proc genCast(p: BProc, e: PNode, d: var TLoc) = const ValueTypes = {tyFloat..tyFloat128, tyTuple, tyObject, tyArray} let destt = skipTypes(e.typ, abstractRange) - srct = skipTypes(e.sons[1].typ, abstractRange) + srct = skipTypes(e[1].typ, abstractRange) if destt.kind in ValueTypes or srct.kind in ValueTypes: # 'cast' and some float type involved? --> use a union. inc(p.labels) @@ -1914,12 +1913,12 @@ proc genCast(p: BProc, e: PNode, d: var TLoc) = var tmp: TLoc tmp.r = "LOC$1.source" % [lbl] linefmt(p, cpsLocals, "union { $1 source; $2 dest; } LOC$3;$n", - [getTypeDesc(p.module, e.sons[1].typ), getTypeDesc(p.module, e.typ), lbl]) + [getTypeDesc(p.module, e[1].typ), getTypeDesc(p.module, e.typ), lbl]) tmp.k = locExpr tmp.lode = lodeTyp srct tmp.storage = OnStack tmp.flags = {} - expr(p, e.sons[1], tmp) + expr(p, e[1], tmp) putIntoDest(p, d, e, "LOC$#.dest" % [lbl], tmp.storage) else: # I prefer the shorter cast version for pointer types -> generate less @@ -1931,27 +1930,27 @@ proc genRangeChck(p: BProc, n: PNode, d: var TLoc, magic: string) = var dest = skipTypes(n.typ, abstractVar) if optRangeCheck notin p.options or (dest.kind in {tyUInt..tyUInt64} and checkUnsignedConversions notin p.config.legacyFeatures): - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) putIntoDest(p, d, n, "(($1) ($2))" % [getTypeDesc(p.module, dest), rdCharLoc(a)], a.storage) else: let mm = if dest.kind in {tyUInt32, tyUInt64, tyUInt}: "chckRangeU" else: magic - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) putIntoDest(p, d, lodeTyp dest, ropecg(p.module, "(($1)#$5($2, $3, $4))", [ getTypeDesc(p.module, dest), rdCharLoc(a), - genLiteral(p, n.sons[1], dest), genLiteral(p, n.sons[2], dest), + genLiteral(p, n[1], dest), genLiteral(p, n[2], dest), mm]), a.storage) proc genConv(p: BProc, e: PNode, d: var TLoc) = let destType = e.typ.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink}) - if sameBackendType(destType, e.sons[1].typ): - expr(p, e.sons[1], d) + if sameBackendType(destType, e[1].typ): + expr(p, e[1], d) else: genSomeCast(p, e, d) proc convStrToCStr(p: BProc, n: PNode, d: var TLoc) = var a: TLoc - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) putIntoDest(p, d, n, ropecg(p.module, "#nimToCStringConv($1)", [rdLoc(a)]), # "($1 ? $1->data : (NCSTRING)\"\")" % [a.rdLoc], @@ -1959,7 +1958,7 @@ proc convStrToCStr(p: BProc, n: PNode, d: var TLoc) = proc convCStrToStr(p: BProc, n: PNode, d: var TLoc) = var a: TLoc - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) putIntoDest(p, d, n, ropecg(p.module, "#cstrToNimstr($1)", [rdLoc(a)]), a.storage) @@ -1967,14 +1966,14 @@ proc convCStrToStr(p: BProc, n: PNode, d: var TLoc) = proc genStrEquals(p: BProc, e: PNode, d: var TLoc) = var x: TLoc - var a = e.sons[1] - var b = e.sons[2] + var a = e[1] + var b = e[2] if a.kind in {nkStrLit..nkTripleStrLit} and a.strVal == "": - initLocExpr(p, e.sons[2], x) + initLocExpr(p, e[2], x) putIntoDest(p, d, e, ropecg(p.module, "($1 == 0)", [lenExpr(p, x)])) elif b.kind in {nkStrLit..nkTripleStrLit} and b.strVal == "": - initLocExpr(p, e.sons[1], x) + initLocExpr(p, e[1], x) putIntoDest(p, d, e, ropecg(p.module, "($1 == 0)", [lenExpr(p, x)])) else: @@ -1984,10 +1983,10 @@ proc binaryFloatArith(p: BProc, e: PNode, d: var TLoc, m: TMagic) = if {optNaNCheck, optInfCheck} * p.options != {}: const opr: array[mAddF64..mDivF64, string] = ["+", "-", "*", "/"] var a, b: TLoc - assert(e.sons[1].typ != nil) - assert(e.sons[2].typ != nil) - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + assert(e[1].typ != nil) + assert(e[2].typ != nil) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) putIntoDest(p, d, e, ropecg(p.module, "(($4)($2) $1 ($4)($3))", [opr[m], rdLoc(a), rdLoc(b), getSimpleTypeDesc(p.module, e[1].typ)])) @@ -2100,21 +2099,21 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = "subInt64"] const fun: array[mInc..mDec, string] = ["addInt", "subInt"] - let underlying = skipTypes(e.sons[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent, tyRange}) + let underlying = skipTypes(e[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent, tyRange}) if optOverflowCheck notin p.options or underlying.kind in {tyUInt..tyUInt64}: binaryStmt(p, e, d, opr[op]) else: var a, b: TLoc - assert(e.sons[1].typ != nil) - assert(e.sons[2].typ != nil) - initLocExpr(p, e.sons[1], a) - initLocExpr(p, e.sons[2], b) + assert(e[1].typ != nil) + assert(e[2].typ != nil) + initLocExpr(p, e[1], a) + initLocExpr(p, e[2], b) - let ranged = skipTypes(e.sons[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent}) + let ranged = skipTypes(e[1].typ, {tyGenericInst, tyAlias, tySink, tyVar, tyLent}) let res = binaryArithOverflowRaw(p, ranged, a, b, if underlying.kind == tyInt64: fun64[op] else: fun[op]) - putIntoDest(p, a, e.sons[1], "($#)($#)" % [ + putIntoDest(p, a, e[1], "($#)($#)" % [ getTypeDesc(p.module, ranged), res]) of mConStrStr: genStrConcat(p, e, d) @@ -2124,14 +2123,14 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = else: var dest, b, call: TLoc initLoc(call, locCall, e, OnHeap) - initLocExpr(p, e.sons[1], dest) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[1], dest) + initLocExpr(p, e[2], b) call.r = ropecg(p.module, "#addChar($1, $2)", [rdLoc(dest), rdLoc(b)]) genAssignment(p, dest, call, {}) of mAppendStrStr: genStrAppend(p, e, d) of mAppendSeqElem: if optSeqDestructors in p.config.globalOptions: - e.sons[1] = makeAddr(e[1]) + e[1] = makeAddr(e[1]) genCall(p, e, d) else: genSeqElemAppend(p, e, d) @@ -2145,7 +2144,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mCharToStr: genDollar(p, e, d, "#nimCharToStr($1)") of mFloatToStr: genDollar(p, e, d, "#nimFloatToStr($1)") of mCStrToStr: genDollar(p, e, d, "#cstrToNimstr($1)") - of mStrToStr, mUnown: expr(p, e.sons[1], d) + of mStrToStr, mUnown: expr(p, e[1], d) of mEnumToStr: if optTinyRtti in p.config.globalOptions: genEnumToStr(p, e, d) @@ -2157,10 +2156,10 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mNewSeq: genNewSeq(p, e) of mNewSeqOfCap: genNewSeqOfCap(p, e, d) of mSizeOf: - let t = e.sons[1].typ.skipTypes({tyTypeDesc}) + let t = e[1].typ.skipTypes({tyTypeDesc}) putIntoDest(p, d, e, "((NI)sizeof($1))" % [getTypeDesc(p.module, t)]) of mAlignOf: - let t = e.sons[1].typ.skipTypes({tyTypeDesc}) + let t = e[1].typ.skipTypes({tyTypeDesc}) if not p.module.compileToCpp: p.module.includeHeader("") putIntoDest(p, d, e, "((NI)alignof($1))" % [getTypeDesc(p.module, t)]) @@ -2209,7 +2208,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mCopyStr, mCopyStrLast: genCall(p, e, d) of mNewString, mNewStringOfCap, mExit, mParseBiggestFloat: - var opr = e.sons[0].sym + var opr = e[0].sym # Why would anyone want to set nodecl to one of these hardcoded magics? # - not sure, and it wouldn't work if the symbol behind the magic isn't # somehow forward-declared from some other usage, but it is *possible* @@ -2230,7 +2229,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = discard cgsym(p.module, $opr.loc.r) # make sure we have pointer-initialising code for hot code reloading if not wasDeclared and p.hcrOn: - addf(p.module.s[cfsDynLibInit], "\t$1 = ($2) hcrGetProc($3, \"$1\");$n", + p.module.s[cfsDynLibInit].addf("\t$1 = ($2) hcrGetProc($3, \"$1\");$n", [mangleDynLibProc(prc), getTypeDesc(p.module, prc.loc.t), getModuleDllPath(p.module, prc)]) genCall(p, e, d) of mDefault: genDefault(p, e, d) @@ -2238,7 +2237,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = of mEcho: genEcho(p, e[1].skipConv) of mArrToSeq: genArrToSeq(p, e, d) of mNLen..mNError, mSlurp..mQuoteAst: - localError(p.config, e.info, strutils.`%`(errXMustBeCompileTime, e.sons[0].sym.name.s)) + localError(p.config, e.info, strutils.`%`(errXMustBeCompileTime, e[0].sym.name.s)) of mSpawn: when defined(leanCompiler): quit "compiler built without support for the 'spawn' statement" @@ -2255,7 +2254,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = var a, b: TLoc let x = if e[1].kind in {nkAddr, nkHiddenAddr}: e[1][0] else: e[1] initLocExpr(p, x, a) - initLocExpr(p, e.sons[2], b) + initLocExpr(p, e[2], b) genDeepCopy(p, a, b) of mDotDot, mEqCString: genCall(p, e, d) of mWasMoved: genWasMoved(p, e) @@ -2288,8 +2287,8 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) = for it in e.sons: if it.kind == nkRange: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), idx) # our counter - initLocExpr(p, it.sons[0], a) - initLocExpr(p, it.sons[1], b) + initLocExpr(p, it[0], a) + initLocExpr(p, it[1], b) lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" & "$2[(NU)($1)>>3] |=(1U<<((NU)($1)&7U));$n", [rdLoc(idx), rdLoc(d), rdSetElemLoc(p.config, a, e.typ), rdSetElemLoc(p.config, b, e.typ)]) @@ -2304,8 +2303,8 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) = for it in e.sons: if it.kind == nkRange: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), idx) # our counter - initLocExpr(p, it.sons[0], a) - initLocExpr(p, it.sons[1], b) + initLocExpr(p, it[0], a) + initLocExpr(p, it[1], b) lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" & "$2 |=(($5)(1)<<(($1)%(sizeof($5)*8)));$n", [ rdLoc(idx), rdLoc(d), rdSetElemLoc(p.config, a, e.typ), @@ -2322,17 +2321,17 @@ proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) = let t = n.typ discard getTypeDesc(p.module, t) # so that any fields are initialized if d.k == locNone: getTemp(p, t, d) - for i in 0 ..< len(n): - var it = n.sons[i] - if it.kind == nkExprColonExpr: it = it.sons[1] + for i in 0.. 0: exprOrStmt if frameName != nil: - add p.s(cpsStmts), deinitFrameNoDebug(p, frameName) + p.s(cpsStmts).add deinitFrameNoDebug(p, frameName) proc genStmtListExpr(p: BProc, n: PNode, d: var TLoc) = genStmtListExprImpl: - expr(p, n[n.len - 1], d) + expr(p, n[^1], d) proc genStmtList(p: BProc, n: PNode) = genStmtListExprImpl: - genStmts(p, n[n.len - 1]) + genStmts(p, n[^1]) proc upConv(p: BProc, n: PNode, d: var TLoc) = var a: TLoc - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) let dest = skipTypes(n.typ, abstractPtrs) if optObjCheck in p.options and not isObjLackingTypeField(dest): var r = rdLoc(a) @@ -2419,9 +2418,9 @@ proc upConv(p: BProc, n: PNode, d: var TLoc) = t = skipTypes(t.lastSon, abstractInst) discard getTypeDesc(p.module, t) if not p.module.compileToCpp: - while t.kind == tyObject and t.sons[0] != nil: - add(r, ".Sup") - t = skipTypes(t.sons[0], skipPtrs) + while t.kind == tyObject and t[0] != nil: + r.add(".Sup") + t = skipTypes(t[0], skipPtrs) let checkFor = if optTinyRtti in p.config.globalOptions: genTypeInfo2Name(p.module, dest) else: @@ -2432,7 +2431,7 @@ proc upConv(p: BProc, n: PNode, d: var TLoc) = else: linefmt(p, cpsStmts, "#chckObj($1.m_type, $2);$n", [r, checkFor]) - if n.sons[0].typ.kind != tyObject: + if n[0].typ.kind != tyObject: putIntoDest(p, d, n, "(($1) ($2))" % [getTypeDesc(p.module, n.typ), rdLoc(a)], a.storage) else: @@ -2442,12 +2441,12 @@ proc upConv(p: BProc, n: PNode, d: var TLoc) = proc downConv(p: BProc, n: PNode, d: var TLoc) = if p.module.compileToCpp: discard getTypeDesc(p.module, skipTypes(n[0].typ, abstractPtrs)) - expr(p, n.sons[0], d) # downcast does C++ for us + expr(p, n[0], d) # downcast does C++ for us else: var dest = skipTypes(n.typ, abstractPtrs) - var arg = n.sons[0] - while arg.kind == nkObjDownConv: arg = arg.sons[0] + var arg = n[0] + while arg.kind == nkObjDownConv: arg = arg[0] var src = skipTypes(arg.typ, abstractPtrs) discard getTypeDesc(p.module, src) @@ -2456,10 +2455,10 @@ proc downConv(p: BProc, n: PNode, d: var TLoc) = var r = rdLoc(a) let isRef = skipTypes(arg.typ, abstractInstOwned).kind in {tyRef, tyPtr, tyVar, tyLent} if isRef: - add(r, "->Sup") + r.add("->Sup") else: - add(r, ".Sup") - for i in 2 .. abs(inheritanceDiff(dest, src)): add(r, ".Sup") + r.add(".Sup") + for i in 2..abs(inheritanceDiff(dest, src)): r.add(".Sup") if isRef: # it can happen that we end up generating '&&x->Sup' here, so we pack # the '&x->Sup' into a temporary and then those address is taken @@ -2484,7 +2483,7 @@ proc exprComplexConst(p: BProc, n: PNode, d: var TLoc) = if id == p.module.labels: # expression not found in the cache: inc(p.module.labels) - addf(p.module.s[cfsData], "NIM_CONST $1 $2 = $3;$n", + p.module.s[cfsData].addf("NIM_CONST $1 $2 = $3;$n", [getTypeDesc(p.module, t), tmp, genConstExpr(p, n)]) if d.k == locNone: @@ -2574,7 +2573,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit: genLineDir(p, n) # may be redundant, it is generated in fixupCall as well - let op = n.sons[0] + let op = n[0] if n.typ.isNil: # discard the value: var a: TLoc @@ -2621,7 +2620,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = of nkIfExpr, nkIfStmt: genIf(p, n, d) of nkWhen: # This should be a "when nimvm" node. - expr(p, n.sons[1].sons[0], d) + expr(p, n[1][0], d) of nkObjDownConv: downConv(p, n, d) of nkObjUpConv: upConv(p, n, d) of nkChckRangeF: genRangeChck(p, n, d, "chckRangeF") @@ -2630,7 +2629,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = of nkStringToCString: convStrToCStr(p, n, d) of nkCStringToString: convCStrToStr(p, n, d) of nkLambdaKinds: - var sym = n.sons[namePos].sym + var sym = n[namePos].sym genProc(p.module, sym) if sym.loc.r == nil or sym.loc.lode == nil: internalError(p.config, n.info, "expr: proc not init " & sym.name.s) @@ -2678,8 +2677,8 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = of nkPragma: genPragma(p, n) of nkPragmaBlock: expr(p, n.lastSon, d) of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef: - if n.sons[genericParamsPos].kind == nkEmpty: - var prc = n.sons[namePos].sym + if n[genericParamsPos].kind == nkEmpty: + var prc = n[namePos].sym # due to a bug/limitation in the lambda lifting, unused inner procs # are not transformed correctly. We work around this issue (#411) here # by ensuring it's no inner proc (owner is a module): @@ -2699,7 +2698,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = else: internalError(p.config, n.info, "expr(" & $n.kind & "); unknown node kind") proc genNamedConstExpr(p: BProc, n: PNode): Rope = - if n.kind == nkExprColonExpr: result = genConstExpr(p, n.sons[1]) + if n.kind == nkExprColonExpr: result = genConstExpr(p, n[1]) else: result = genConstExpr(p, n) proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope = @@ -2728,9 +2727,9 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope = result = rope"{}" of tyTuple: result = rope"{" - for i in 0 ..< typ.len: + for i in 0.. 0: result.add ", " - result.add getDefaultValue(p, typ.sons[i], info) + result.add getDefaultValue(p, typ[i], info) result.add "}" of tyArray: result = rope"{}" of tySet: @@ -2746,9 +2745,9 @@ proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, for it in obj.sons: getNullValueAux(p, t, it, cons, result, count) of nkRecCase: - getNullValueAux(p, t, obj.sons[0], cons, result, count) - for i in 1 ..< len(obj): - getNullValueAux(p, t, lastSon(obj.sons[i]), cons, result, count) + getNullValueAux(p, t, obj[0], cons, result, count) + for i in 1.. 0: result.add ", " inc count @@ -2768,7 +2767,7 @@ proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, result: var Rope; count: var int) = - var base = t.sons[0] + var base = t[0] let oldRes = result if not p.module.compileToCpp: result.add "{" let oldcount = count @@ -2776,7 +2775,7 @@ proc getNullValueAuxT(p: BProc; orig, t: PType; obj, cons: PNode, base = skipTypes(base, skipPtrs) getNullValueAuxT(p, orig, base, base.n, cons, result, count) elif not isObjLackingTypeField(t) and not p.module.compileToCpp: - addf(result, "$1", [genTypeInfo(p.module, orig, obj.info)]) + result.addf("$1", [genTypeInfo(p.module, orig, obj.info)]) inc count getNullValueAux(p, t, obj, cons, result, count) # do not emit '{}' as that is not valid C: @@ -2788,34 +2787,33 @@ proc genConstObjConstr(p: BProc; n: PNode): Rope = let t = n.typ.skipTypes(abstractInstOwned) var count = 0 #if not isObjLackingTypeField(t) and not p.module.compileToCpp: - # addf(result, "{$1}", [genTypeInfo(p.module, t)]) + # result.addf("{$1}", [genTypeInfo(p.module, t)]) # inc count getNullValueAuxT(p, t, t, t.n, n, result, count) if p.module.compileToCpp: result = "{$1}$n" % [result] proc genConstSimpleList(p: BProc, n: PNode): Rope = - var length = len(n) result = rope("{") - for i in 0 .. length - 2: - addf(result, "$1,$n", [genNamedConstExpr(p, n.sons[i])]) - if length > 0: - add(result, genNamedConstExpr(p, n.sons[length - 1])) - addf(result, "}$n", []) + for i in 0.. 0: + result.add(genNamedConstExpr(p, n[^1])) + result.addf("}$n", []) proc genConstSeq(p: BProc, n: PNode, t: PType): Rope = var data = "{{$1, $1 | NIM_STRLIT_FLAG}" % [n.len.rope] if n.len > 0: # array part needs extra curlies: data.add(", {") - for i in 0 ..< n.len: + for i in 0.. 0: data.addf(",$n", []) - data.add genConstExpr(p, n.sons[i]) + data.add genConstExpr(p, n[i]) data.add("}") data.add("}") result = getTempName(p.module) - let base = t.skipTypes(abstractInst).sons[0] + let base = t.skipTypes(abstractInst)[0] appcg(p.module, cfsData, "NIM_CONST struct {$n" & @@ -2828,25 +2826,25 @@ proc genConstSeq(p: BProc, n: PNode, t: PType): Rope = proc genConstSeqV2(p: BProc, n: PNode, t: PType): Rope = var data = rope"{" - for i in 0 ..< n.len: + for i in 0.. 0: data.addf(",$n", []) - data.add genConstExpr(p, n.sons[i]) + data.add genConstExpr(p, n[i]) data.add("}") let payload = getTempName(p.module) - let base = t.skipTypes(abstractInst).sons[0] + let base = t.skipTypes(abstractInst)[0] appcg(p.module, cfsData, "static const struct {$n" & " NI cap; void* allocator; $1 data[$2];$n" & "} $3 = {$2, NIM_NIL, $4};$n", [ - getTypeDesc(p.module, base), len(n), payload, data]) - result = "{$1, ($2*)&$3}" % [rope(len(n)), getSeqPayloadType(p.module, t), payload] + getTypeDesc(p.module, base), n.len, payload, data]) + result = "{$1, ($2*)&$3}" % [rope(n.len), getSeqPayloadType(p.module, t), payload] proc genConstExpr(p: BProc, n: PNode): Rope = case n.kind of nkHiddenStdConv, nkHiddenSubConv: - result = genConstExpr(p, n.sons[1]) + result = genConstExpr(p, n[1]) of nkCurly: var cs: TBitSet toBitSet(p.config, n, cs) @@ -2859,7 +2857,7 @@ proc genConstExpr(p: BProc, n: PNode): Rope = else: result = genConstSeq(p, n, n.typ) elif t.kind == tyProc and t.callConv == ccClosure and n.len > 1 and - n.sons[1].kind == nkNilLit: + n[1].kind == nkNilLit: # Conversion: nimcall -> closure. # this hack fixes issue that nkNilLit is expanded to {NIM_NIL,NIM_NIL} # this behaviour is needed since closure_var = nil must be diff --git a/compiler/ccgliterals.nim b/compiler/ccgliterals.nim index 0dd12452c..a016bdcaa 100644 --- a/compiler/ccgliterals.nim +++ b/compiler/ccgliterals.nim @@ -35,8 +35,8 @@ proc detectSeqVersion(m: BModule): int = proc genStringLiteralDataOnlyV1(m: BModule, s: string): Rope = discard cgsym(m, "TGenericSeq") result = getTempName(m) - addf(m.s[cfsData], "STRING_LITERAL($1, $2, $3);$n", - [result, makeCString(s), rope(len(s))]) + m.s[cfsData].addf("STRING_LITERAL($1, $2, $3);$n", + [result, makeCString(s), rope(s.len)]) proc genStringLiteralV1(m: BModule; n: PNode): Rope = if s.isNil: @@ -54,10 +54,10 @@ proc genStringLiteralV1(m: BModule; n: PNode): Rope = # ------ Version 2: destructor based strings and seqs ----------------------- proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope) = - addf(m.s[cfsData], "static const struct {$n" & + m.s[cfsData].addf("static const struct {$n" & " NI cap; void* allocator; NIM_CHAR data[$2+1];$n" & "} $1 = { $2, NIM_NIL, $3 };$n", - [result, rope(len(s)), makeCString(s)]) + [result, rope(s.len), makeCString(s)]) proc genStringLiteralV2(m: BModule; n: PNode): Rope = let id = nodeTableTestOrSet(m.dataCache, n, m.labels) @@ -68,12 +68,12 @@ proc genStringLiteralV2(m: BModule; n: PNode): Rope = discard cgsym(m, "NimStrPayload") discard cgsym(m, "NimStringV2") # string literal not found in the cache: - addf(m.s[cfsData], "static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n", - [result, rope(len(n.strVal)), pureLit]) + m.s[cfsData].addf("static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n", + [result, rope(n.strVal.len), pureLit]) else: result = getTempName(m) - addf(m.s[cfsData], "static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n", - [result, rope(len(n.strVal)), m.tmpBase & rope(id)]) + m.s[cfsData].addf("static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n", + [result, rope(n.strVal.len), m.tmpBase & rope(id)]) proc genStringLiteralV2Const(m: BModule; n: PNode): Rope = let id = nodeTableTestOrSet(m.dataCache, n, m.labels) @@ -86,7 +86,7 @@ proc genStringLiteralV2Const(m: BModule; n: PNode): Rope = genStringLiteralDataOnlyV2(m, n.strVal, pureLit) else: pureLit = m.tmpBase & rope(id) - result = "{$1, (NimStrPayload*)&$2}" % [rope(len(n.strVal)), pureLit] + result = "{$1, (NimStrPayload*)&$2}" % [rope(n.strVal.len), pureLit] # ------ Version selector --------------------------------------------------- diff --git a/compiler/ccgmerge.nim b/compiler/ccgmerge.nim index cdb43f20f..2d7e0bda7 100644 --- a/compiler/ccgmerge.nim +++ b/compiler/ccgmerge.nim @@ -50,9 +50,9 @@ const proc genSectionStart*(fs: TCFileSection; conf: ConfigRef): Rope = if compilationCachePresent(conf): result = nil - add(result, "\n/*\t") - add(result, CFileSectionNames[fs]) - add(result, ":*/\n") + result.add("\n/*\t") + result.add(CFileSectionNames[fs]) + result.add(":*/\n") proc genSectionEnd*(fs: TCFileSection; conf: ConfigRef): Rope = if compilationCachePresent(conf): @@ -61,9 +61,9 @@ proc genSectionEnd*(fs: TCFileSection; conf: ConfigRef): Rope = proc genSectionStart*(ps: TCProcSection; conf: ConfigRef): Rope = if compilationCachePresent(conf): result = rope("") - add(result, "\n/*\t") - add(result, CProcSectionNames[ps]) - add(result, ":*/\n") + result.add("\n/*\t") + result.add(CProcSectionNames[ps]) + result.add(":*/\n") proc genSectionEnd*(ps: TCProcSection; conf: ConfigRef): Rope = if compilationCachePresent(conf): diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index a3d807e26..130c41011 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -46,7 +46,7 @@ proc inExceptBlockLen(p: BProc): int = proc startBlockInternal(p: BProc): int {.discardable.} = inc(p.labels) - result = len(p.blocks) + result = p.blocks.len setLen(p.blocks, result + 1) p.blocks[result].id = p.labels p.blocks[result].nestedTryStmts = p.nestedTryStmts.len.int16 @@ -62,16 +62,15 @@ proc endBlock(p: BProc) proc genVarTuple(p: BProc, n: PNode) = var tup, field: TLoc if n.kind != nkVarTuple: internalError(p.config, n.info, "genVarTuple") - var L = len(n) # if we have a something that's been captured, use the lowering instead: - for i in 0 .. L-3: + for i in 0..= 2 and n[1].kind == nkIntLit: statesCounter = getInt(n[1]) let prefix = if n.len == 3 and n[2].kind == nkStrLit: n[2].strVal.rope else: rope"STATE" - for i in 0i64 .. toInt64(statesCounter): + for i in 0i64..toInt64(statesCounter): lineF(p, cpsStmts, "case $2: goto $1$2;$n", [prefix, rope(i)]) lineF(p, cpsStmts, "}$n", []) @@ -257,11 +256,11 @@ proc genBreakState(p: BProc, n: PNode, d: var TLoc) = var a: TLoc initLoc(d, locExpr, n, OnUnknown) - if n.sons[0].kind == nkClosure: - initLocExpr(p, n.sons[0].sons[1], a) + if n[0].kind == nkClosure: + initLocExpr(p, n[0][1], a) d.r = "(((NI*) $1)[1] < 0)" % [rdLoc(a)] else: - initLocExpr(p, n.sons[0], a) + initLocExpr(p, n[0], a) # the environment is guaranteed to contain the 'state' field at offset 1: d.r = "((((NI*) $1.ClE_0)[1]) < 0)" % [rdLoc(a)] @@ -322,12 +321,12 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) = if value.kind in nkCallKinds and value[0].kind == nkSym and sfConstructor in value[0].sym.flags: var params: Rope - let typ = skipTypes(value.sons[0].typ, abstractInst) + let typ = skipTypes(value[0].typ, abstractInst) assert(typ.kind == tyProc) for i in 1.. 1: + if n.len > 1: lineF(p, cpsStmts, "goto $1;$n", [lend]) fixLabel(p, lelse) elif it.len == 1: startBlock(p) - expr(p, it.sons[0], d) + expr(p, it[0], d) endBlock(p) else: internalError(p.config, n.info, "genIf()") - if len(n) > 1: fixLabel(p, lend) + if n.len > 1: fixLabel(p, lend) proc genReturnStmt(p: BProc, t: PNode) = if nfPreventCg in t.flags: return p.beforeRetNeeded = true genLineDir(p, t) - if (t.sons[0].kind != nkEmpty): genStmts(p, t.sons[0]) + if (t[0].kind != nkEmpty): genStmts(p, t[0]) blockLeaveActions(p, howManyTrys = p.nestedTryStmts.len, howManyExcepts = p.inExceptBlockLen) if (p.finallySafePoints.len > 0) and not p.noSafePoints: # If we're in a finally block, and we came here by exception # consume it before we return. - var safePoint = p.finallySafePoints[p.finallySafePoints.len-1] + var safePoint = p.finallySafePoints[^1] linefmt(p, cpsStmts, "if ($1.status != 0) #popCurrentException();$n", [safePoint]) lineF(p, cpsStmts, "goto BeforeRet_;$n", []) proc genGotoForCase(p: BProc; caseStmt: PNode) = - for i in 1 ..< caseStmt.len: + for i in 1.. 10_000: localError(p.config, it.info, "case statement has too many cases for computed goto"); return arraySize = toInt(aSize) - if firstOrd(p.config, it.sons[0].typ) != 0: + if firstOrd(p.config, it[0].typ) != 0: localError(p.config, it.info, "case statement has to start at 0 for computed goto"); return if casePos < 0: @@ -510,54 +509,54 @@ proc genComputedGoto(p: BProc; n: PNode) = gotoArray.addf("&&TMP$#_};$n", [rope(id+arraySize)]) line(p, cpsLocals, gotoArray) - for j in 0 ..< casePos: - genStmts(p, n.sons[j]) + for j in 0.. stringCaseThreshold: var bitMask = math.nextPowerOfTwo(strings) - 1 var branches: seq[Rope] newSeq(branches, bitMask + 1) var a: TLoc - initLocExpr(p, t.sons[0], a) # fist pass: generate ifs+goto: + initLocExpr(p, t[0], a) # fist pass: generate ifs+goto: var labId = p.labels - for i in 1 ..< len(t): + for i in 1.. RangeExpandLimit: + if (b[i].kind == nkRange) and + b[i][1].intVal - b[i][0].intVal > RangeExpandLimit: return true proc ifSwitchSplitPoint(p: BProc, n: PNode): int = - for i in 1..n.len-1: + for i in 1.. 0: genIfForCaseUntil(p, n, d, rangeFormat = "if ($1 >= $2 && $1 <= $3) goto $4;$n", eqFormat = "if ($1 == $2) goto $3;$n", @@ -859,7 +853,7 @@ proc genOrdinalCase(p: BProc, n: PNode, d: var TLoc) = if splitPoint+1 < n.len: lineF(p, cpsStmts, "switch ($1) {$n", [rdCharLoc(a)]) var hasDefault = false - for i in splitPoint+1 ..< n.len: + for i in splitPoint+1..= $2 && $1 <= $3) goto $4;$n", "if ($1 == $2) goto $3;$n") else: - if t.sons[0].kind == nkSym and sfGoto in t.sons[0].sym.flags: + if t[0].kind == nkSym and sfGoto in t[0].sym.flags: genGotoForCase(p, t) else: genOrdinalCase(p, t, d) @@ -926,7 +920,7 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) = genLineDir(p, t) discard cgsym(p.module, "popCurrentExceptionEx") let fin = if t[^1].kind == nkFinally: t[^1] else: nil - add(p.nestedTryStmts, (fin, false)) + p.nestedTryStmts.add((fin, false)) startBlock(p, "try {$n") expr(p, t[0], d) endBlock(p) @@ -947,7 +941,7 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) = genExceptBranchBody(t[i][0]) endBlock(p) else: - for j in 0..t[i].len-2: + for j in 0.. 1: lineF(p, cpsStmts, "else", []) startBlock(p) if not quirkyExceptions: linefmt(p, cpsStmts, "$1.status = 0;$n", [safePoint]) - expr(p, t.sons[i].sons[0], d) + expr(p, t[i][0], d) linefmt(p, cpsStmts, "#popCurrentException();$n", []) endBlock(p) else: var orExpr: Rope = nil - for j in 0 .. blen - 2: - assert(t.sons[i].sons[j].kind == nkType) - if orExpr != nil: add(orExpr, "||") + for j in 0..= 1 and n.sons[0].kind in {nkStrLit..nkTripleStrLit}: - let sec = n.sons[0].strVal + if n.len >= 1 and n[0].kind in {nkStrLit..nkTripleStrLit}: + let sec = n[0].strVal if sec.startsWith("/*TYPESECTION*/"): result = cfsTypes elif sec.startsWith("/*VARSECTION*/"): result = cfsVars elif sec.startsWith("/*INCLUDESECTION*/"): result = cfsHeaders proc genEmit(p: BProc, t: PNode) = - var s = genAsmOrEmitStmt(p, t.sons[1]) + var s = genAsmOrEmitStmt(p, t[1]) if p.prc == nil: # top level emit pragma? let section = determineSection(t[1]) genCLineDir(p.module.s[section], t.info, p.config) - add(p.module.s[section], s) + p.module.s[section].add(s) else: genLineDir(p, t) line(p, cpsStmts, s) @@ -1169,18 +1161,18 @@ proc genPragma(p: BProc, n: PNode) = of wInjectStmt: var p = newProc(nil, p.module) p.options = p.options - {optLineTrace, optStackTrace} - genStmts(p, it.sons[1]) + genStmts(p, it[1]) p.module.injectStmt = p.s(cpsStmts) else: discard proc fieldDiscriminantCheckNeeded(p: BProc, asgn: PNode): bool = if optFieldCheck in p.options: - var le = asgn.sons[0] + var le = asgn[0] if le.kind == nkCheckedFieldExpr: - var field = le.sons[0].sons[1].sym + var field = le[0][1].sym result = sfDiscriminant in field.flags elif le.kind == nkDotExpr: - var field = le.sons[1].sym + var field = le[1].sym result = sfDiscriminant in field.flags proc genDiscriminantCheck(p: BProc, a, tmp: TLoc, objtype: PType, @@ -1188,14 +1180,13 @@ proc genDiscriminantCheck(p: BProc, a, tmp: TLoc, objtype: PType, var t = skipTypes(objtype, abstractVar) assert t.kind == tyObject discard genTypeInfo(p.module, t, a.lode.info) - var L = toInt64(lengthOrd(p.config, field.typ)) if not containsOrIncl(p.module.declaredThings, field.id): appcg(p.module, cfsVars, "extern $1", [discriminatorTableDecl(p.module, t, field)]) lineCg(p, cpsStmts, "#FieldDiscriminantCheck((NI)(NU)($1), (NI)(NU)($2), $3, $4);$n", [rdLoc(a), rdLoc(tmp), discriminatorTableName(p.module, t, field), - intLiteral(L+1)]) + intLiteral(toInt64(lengthOrd(p.config, field.typ))+1)]) proc genCaseObjDiscMapping(p: BProc, e: PNode, t: PType, field: PSym; d: var TLoc) = const ObjDiscMappingProcSlot = -5 @@ -1214,12 +1205,12 @@ proc genCaseObjDiscMapping(p: BProc, e: PNode, t: PType, field: PSym; d: var TLo proc asgnFieldDiscriminant(p: BProc, e: PNode) = var a, tmp: TLoc - var dotExpr = e.sons[0] - if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr.sons[0] - initLocExpr(p, e.sons[0], a) + var dotExpr = e[0] + if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr[0] + initLocExpr(p, e[0], a) getTemp(p, a.t, tmp) - expr(p, e.sons[1], tmp) - let field = dotExpr.sons[1].sym + expr(p, e[1], tmp) + let field = dotExpr[1].sym if optTinyRtti in p.config.globalOptions: let t = dotExpr[0].typ.skipTypes(abstractInst) var oldVal, newVal: TLoc @@ -1229,13 +1220,13 @@ proc asgnFieldDiscriminant(p: BProc, e: PNode) = "#nimFieldDiscriminantCheckV2($1, $2);$n", [rdLoc(oldVal), rdLoc(newVal)]) else: - genDiscriminantCheck(p, a, tmp, dotExpr.sons[0].typ, field) + genDiscriminantCheck(p, a, tmp, dotExpr[0].typ, field) genAssignment(p, a, tmp, {}) proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) = - if e.sons[0].kind == nkSym and sfGoto in e.sons[0].sym.flags: + if e[0].kind == nkSym and sfGoto in e[0].sym.flags: genLineDir(p, e) - genGotoVar(p, e.sons[1]) + genGotoVar(p, e[1]) elif not fieldDiscriminantCheckNeeded(p, e): let le = e[0] let ri = e[1] diff --git a/compiler/ccgthreadvars.nim b/compiler/ccgthreadvars.nim index d2d4ada36..7586aa865 100644 --- a/compiler/ccgthreadvars.nim +++ b/compiler/ccgthreadvars.nim @@ -19,8 +19,8 @@ proc accessThreadLocalVar(p: BProc, s: PSym) = if emulatedThreadVars(p.config) and not p.threadVarAccessed: p.threadVarAccessed = true incl p.module.flags, usesThreadVars - addf(p.procSec(cpsLocals), "\tNimThreadVars* NimTV_;$n", []) - add(p.procSec(cpsInit), + p.procSec(cpsLocals).addf("\tNimThreadVars* NimTV_;$n", []) + p.procSec(cpsInit).add( ropecg(p.module, "\tNimTV_ = (NimThreadVars*) #GetThreadLocalVars();$n", [])) proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) = @@ -30,23 +30,23 @@ proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) = # allocator for it :-( if not containsOrIncl(m.g.nimtvDeclared, s.id): m.g.nimtvDeps.add(s.loc.t) - addf(m.g.nimtv, "$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r]) + m.g.nimtv.addf("$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r]) else: - if isExtern: add(m.s[cfsVars], "extern ") - if optThreads in m.config.globalOptions: add(m.s[cfsVars], "NIM_THREADVAR ") - add(m.s[cfsVars], getTypeDesc(m, s.loc.t)) - addf(m.s[cfsVars], " $1;$n", [s.loc.r]) + if isExtern: m.s[cfsVars].add("extern ") + if optThreads in m.config.globalOptions: m.s[cfsVars].add("NIM_THREADVAR ") + m.s[cfsVars].add(getTypeDesc(m, s.loc.t)) + m.s[cfsVars].addf(" $1;$n", [s.loc.r]) proc generateThreadLocalStorage(m: BModule) = if m.g.nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags): for t in items(m.g.nimtvDeps): discard getTypeDesc(m, t) - addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [m.g.nimtv]) + m.s[cfsSeqTypes].addf("typedef struct {$1} NimThreadVars;$n", [m.g.nimtv]) proc generateThreadVarsSize(m: BModule) = if m.g.nimtv != nil: let externc = if m.config.cmd == cmdCompileToCpp or sfCompileToCpp in m.module.flags: "extern \"C\" " else: "" - addf(m.s[cfsProcs], + m.s[cfsProcs].addf( "$#NI NimThreadVarsSize(){return (NI)sizeof(NimThreadVars);}$n", [externc.rope]) diff --git a/compiler/ccgtrav.nim b/compiler/ccgtrav.nim index e494b3e48..a0048a769 100644 --- a/compiler/ccgtrav.nim +++ b/compiler/ccgtrav.nim @@ -28,18 +28,18 @@ proc genTraverseProc(c: TTraversalClosure, accessor: Rope, n: PNode; if n == nil: return case n.kind of nkRecList: - for i in 0 ..< len(n): - genTraverseProc(c, accessor, n.sons[i], typ) + for i in 0..