Cosmetic compiler cleanup (#12718)

* Cleanup compiler code base

* Unify add calls

* Unify len invocations

* Unify range operators

* Fix oversight

* Remove {.procvar.} pragma

* initCandidate -> newCandidate where reasonable

* Unify safeLen calls
This commit is contained in:
Clyybber 2019-11-28 17:13:04 +01:00 • committed by Andreas Rumpf
commit 7e747d11c6
109 changed files with 6115 additions and 6254 deletions

View file

@ -82,7 +82,7 @@ when defined(nimpretty):
result = (n.info.line.int, n.info.line.int + countLines(n.comment))
else:
result = (n.info.line.int, n.info.line.int)
for i in 0 ..< safeLen(n):
for i in 0..<n.safeLen:
let (currMin, currMax) = minmaxLine(n[i])
if currMin < result[0]: result[0] = currMin
if currMax > result[1]: result[1] = currMax
@ -109,12 +109,11 @@ proc initSrcGen(g: var TSrcGen, renderFlags: TRenderFlags; config: ConfigRef) =
g.config = config
proc addTok(g: var TSrcGen, kind: TTokType, s: string; sym: PSym = nil) =
var length = len(g.tokens)
setLen(g.tokens, length + 1)
g.tokens[length].kind = kind
g.tokens[length].length = int16(len(s))
g.tokens[length].sym = sym
add(g.buf, s)
setLen(g.tokens, g.tokens.len + 1)
g.tokens[^1].kind = kind
g.tokens[^1].length = int16(s.len)
g.tokens[^1].sym = sym
g.buf.add(s)
proc addPendingNL(g: var TSrcGen) =
if g.pendingNL >= 0:
@ -172,17 +171,17 @@ proc dedent(g: var TSrcGen) =
proc put(g: var TSrcGen, kind: TTokType, s: string; sym: PSym = nil) =
if kind != tkSpaces:
addPendingNL(g)
if len(s) > 0:
if s.len > 0:
addTok(g, kind, s, sym)
inc(g.lineLen, len(s))
inc(g.lineLen, s.len)
else:
g.pendingWhitespace = s.len
proc putComment(g: var TSrcGen, s: string) =
if s.len == 0: return
var i = 0
let hi = len(s) - 1
var isCode = (len(s) >= 2) and (s[1] != ' ')
let hi = s.len - 1
var isCode = (s.len >= 2) and (s[1] != ' ')
var ind = g.lineLen
var com = "## "
while i <= hi:
@ -201,7 +200,7 @@ proc putComment(g: var TSrcGen, s: string) =
inc(i)
optNL(g, ind)
of ' ', '\x09':
add(com, s[i])
com.add(s[i])
inc(i)
else:
# we may break the comment into a multi-line comment if the line
@ -214,7 +213,7 @@ proc putComment(g: var TSrcGen, s: string) =
optNL(g, ind)
com = "## "
while i <= hi and s[i] > ' ':
add(com, s[i])
com.add(s[i])
inc(i)
put(g, tkComment, com)
optNL(g)
@ -222,7 +221,7 @@ proc putComment(g: var TSrcGen, s: string) =
proc maxLineLength(s: string): int =
if s.len == 0: return 0
var i = 0
let hi = len(s) - 1
let hi = s.len - 1
var lineLen = 0
while i <= hi:
case s[i]
@ -243,7 +242,7 @@ proc maxLineLength(s: string): int =
proc putRawStr(g: var TSrcGen, kind: TTokType, s: string) =
var i = 0
let hi = len(s) - 1
let hi = s.len - 1
var str = ""
while i <= hi:
case s[i]
@ -259,12 +258,12 @@ proc putRawStr(g: var TSrcGen, kind: TTokType, s: string) =
inc(i)
optNL(g, 0)
else:
add(str, s[i])
str.add(s[i])
inc(i)
put(g, kind, str)
proc containsNL(s: string): bool =
for i in 0 ..< len(s):
for i in 0..<s.len:
case s[i]
of '\x0D', '\x0A':
return true
@ -273,9 +272,8 @@ proc containsNL(s: string): bool =
result = false
proc pushCom(g: var TSrcGen, n: PNode) =
var length = len(g.comStack)
setLen(g.comStack, length + 1)
g.comStack[length] = n
setLen(g.comStack, g.comStack.len + 1)
g.comStack[^1] = n
proc popAllComs(g: var TSrcGen) =
setLen(g.comStack, 0)
@ -292,11 +290,11 @@ proc shouldRenderComment(g: var TSrcGen, n: PNode): bool =
proc gcom(g: var TSrcGen, n: PNode) =
assert(n != nil)
if shouldRenderComment(g, n):
if (g.pendingNL < 0) and (len(g.buf) > 0) and (g.buf[len(g.buf)-1] != ' '):
if (g.pendingNL < 0) and (g.buf.len > 0) and (g.buf[^1] != ' '):
put(g, tkSpaces, Space)
# Before long comments we cannot make sure that a newline is generated,
# because this might be wrong. But it is no problem in practice.
if (g.pendingNL < 0) and (len(g.buf) > 0) and
if (g.pendingNL < 0) and (g.buf.len > 0) and
(g.lineLen < LineCommentColumn):
var ml = maxLineLength(n.comment)
if ml + LineCommentColumn <= MaxLineLen:
@ -304,7 +302,7 @@ proc gcom(g: var TSrcGen, n: PNode) =
putComment(g, n.comment) #assert(g.comStack[high(g.comStack)] = n);
proc gcoms(g: var TSrcGen) =
for i in 0 .. high(g.comStack): gcom(g, g.comStack[i])
for i in 0..high(g.comStack): gcom(g, g.comStack[i])
popAllComs(g)
proc lsub(g: TSrcGen; n: PNode): int
@ -398,8 +396,8 @@ proc atom(g: TSrcGen; n: PNode): string =
proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
assert(theEnd < 0)
result = 0
for i in start .. len(n) + theEnd:
let param = n.sons[i]
for i in start..n.len + theEnd:
let param = n[i]
if nfDefaultParam notin param.flags:
inc(result, lsub(g, param))
inc(result, 2) # for ``, ``
@ -409,7 +407,7 @@ proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
proc lsons(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
assert(theEnd < 0)
result = 0
for i in start .. len(n) + theEnd: inc(result, lsub(g, n.sons[i]))
for i in start..n.len + theEnd: inc(result, lsub(g, n[i]))
proc lsub(g: TSrcGen; n: PNode): int =
# computes the length of a tree
@ -419,17 +417,17 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkEmpty: result = 0
of nkTripleStrLit:
if containsNL(n.strVal): result = MaxLineLen + 1
else: result = len(atom(g, n))
else: result = atom(g, n).len
of succ(nkEmpty)..pred(nkTripleStrLit), succ(nkTripleStrLit)..nkNilLit:
result = len(atom(g, n))
result = atom(g, n).len
of nkCall, nkBracketExpr, nkCurlyExpr, nkConv, nkPattern, nkObjConstr:
result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + 2
result = lsub(g, n[0]) + lcomma(g, n, 1) + 2
of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv: result = lsub(g, n[1])
of nkCast: result = lsub(g, n.sons[0]) + lsub(g, n.sons[1]) + len("cast[]()")
of nkAddr: result = (if n.len>0: lsub(g, n.sons[0]) + len("addr()") else: 4)
of nkStaticExpr: result = lsub(g, n.sons[0]) + len("static_")
of nkHiddenAddr, nkHiddenDeref, nkStringToCString, nkCStringToString: result = lsub(g, n.sons[0])
of nkCommand: result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + 1
of nkCast: result = lsub(g, n[0]) + lsub(g, n[1]) + len("cast[]()")
of nkAddr: result = (if n.len>0: lsub(g, n[0]) + len("addr()") else: 4)
of nkStaticExpr: result = lsub(g, n[0]) + len("static_")
of nkHiddenAddr, nkHiddenDeref, nkStringToCString, nkCStringToString: result = lsub(g, n[0])
of nkCommand: result = lsub(g, n[0]) + lcomma(g, n, 1) + 1
of nkExprEqExpr, nkAsgn, nkFastAsgn: result = lsons(g, n) + 3
of nkPar, nkCurly, nkBracket, nkClosure: result = lcomma(g, n) + 2
of nkTupleConstr:
@ -439,93 +437,92 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkTableConstr:
result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
of nkClosedSymChoice, nkOpenSymChoice:
result = lsons(g, n) + len("()") + len(n) - 1
result = lsons(g, n) + len("()") + n.len - 1
of nkTupleTy: result = lcomma(g, n) + len("tuple[]")
of nkTupleClassTy: result = len("tuple")
of nkDotExpr: result = lsons(g, n) + 1
of nkBind: result = lsons(g, n) + len("bind_")
of nkBindStmt: result = lcomma(g, n) + len("bind_")
of nkMixinStmt: result = lcomma(g, n) + len("mixin_")
of nkCheckedFieldExpr: result = lsub(g, n.sons[0])
of nkCheckedFieldExpr: result = lsub(g, n[0])
of nkLambda: result = lsons(g, n) + len("proc__=_")
of nkDo: result = lsons(g, n) + len("do__:_")
of nkConstDef, nkIdentDefs:
result = lcomma(g, n, 0, - 3)
var L = len(n)
if n.sons[L - 2].kind != nkEmpty: result = result + lsub(g, n.sons[L - 2]) + 2
if n.sons[L - 1].kind != nkEmpty: result = result + lsub(g, n.sons[L - 1]) + 3
if n[^2].kind != nkEmpty: result = result + lsub(g, n[^2]) + 2
if n[^1].kind != nkEmpty: result = result + lsub(g, n[^1]) + 3
of nkVarTuple: result = lcomma(g, n, 0, - 3) + len("() = ") + lsub(g, lastSon(n))
of nkChckRangeF: result = len("chckRangeF") + 2 + lcomma(g, n)
of nkChckRange64: result = len("chckRange64") + 2 + lcomma(g, n)
of nkChckRange: result = len("chckRange") + 2 + lcomma(g, n)
of nkObjDownConv, nkObjUpConv:
result = 2
if len(n) >= 1: result = result + lsub(g, n.sons[0])
if n.len >= 1: result = result + lsub(g, n[0])
result = result + lcomma(g, n, 1)
of nkExprColonExpr: result = lsons(g, n) + 2
of nkInfix: result = lsons(g, n) + 2
of nkPrefix:
result = lsons(g, n)+1+(if n.len > 0 and n.sons[1].kind == nkInfix: 2 else: 0)
result = lsons(g, n)+1+(if n.len > 0 and n[1].kind == nkInfix: 2 else: 0)
of nkPostfix: result = lsons(g, n)
of nkCallStrLit: result = lsons(g, n)
of nkPragmaExpr: result = lsub(g, n.sons[0]) + lcomma(g, n, 1)
of nkPragmaExpr: result = lsub(g, n[0]) + lcomma(g, n, 1)
of nkRange: result = lsons(g, n) + 2
of nkDerefExpr: result = lsub(g, n.sons[0]) + 2
of nkDerefExpr: result = lsub(g, n[0]) + 2
of nkAccQuoted: result = lsons(g, n) + 2
of nkIfExpr:
result = lsub(g, n.sons[0].sons[0]) + lsub(g, n.sons[0].sons[1]) + lsons(g, n, 1) +
result = lsub(g, n[0][0]) + lsub(g, n[0][1]) + lsons(g, n, 1) +
len("if_:_")
of nkElifExpr: result = lsons(g, n) + len("_elif_:_")
of nkElseExpr: result = lsub(g, n.sons[0]) + len("_else:_") # type descriptions
of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n.sons[0]) else: 0)+len("typeof()")
of nkRefTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("ref")
of nkPtrTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("ptr")
of nkVarTy: result = (if n.len > 0: lsub(g, n.sons[0])+1 else: 0) + len("var")
of nkElseExpr: result = lsub(g, n[0]) + len("_else:_") # type descriptions
of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n[0]) else: 0)+len("typeof()")
of nkRefTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ref")
of nkPtrTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ptr")
of nkVarTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("var")
of nkDistinctTy:
result = len("distinct") + (if n.len > 0: lsub(g, n.sons[0])+1 else: 0)
result = len("distinct") + (if n.len > 0: lsub(g, n[0])+1 else: 0)
if n.len > 1:
result += (if n[1].kind == nkWith: len("_with_") else: len("_without_"))
result += lcomma(g, n[1])
of nkStaticTy: result = (if n.len > 0: lsub(g, n.sons[0]) else: 0) +
of nkStaticTy: result = (if n.len > 0: lsub(g, n[0]) else: 0) +
len("static[]")
of nkTypeDef: result = lsons(g, n) + 3
of nkOfInherit: result = lsub(g, n.sons[0]) + len("of_")
of nkOfInherit: result = lsub(g, n[0]) + len("of_")
of nkProcTy: result = lsons(g, n) + len("proc_")
of nkIteratorTy: result = lsons(g, n) + len("iterator_")
of nkSharedTy: result = lsons(g, n) + len("shared_")
of nkEnumTy:
if len(n) > 0:
result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + len("enum_")
if n.len > 0:
result = lsub(g, n[0]) + lcomma(g, n, 1) + len("enum_")
else:
result = len("enum")
of nkEnumFieldDef: result = lsons(g, n) + 3
of nkVarSection, nkLetSection:
if len(n) > 1: result = MaxLineLen + 1
if n.len > 1: result = MaxLineLen + 1
else: result = lsons(g, n) + len("var_")
of nkUsingStmt:
if len(n) > 1: result = MaxLineLen + 1
if n.len > 1: result = MaxLineLen + 1
else: result = lsons(g, n) + len("using_")
of nkReturnStmt:
if n.len > 0 and n[0].kind == nkAsgn:
result = len("return_") + lsub(g, n[0][1])
else:
result = len("return_") + lsub(g, n[0])
of nkRaiseStmt: result = lsub(g, n.sons[0]) + len("raise_")
of nkYieldStmt: result = lsub(g, n.sons[0]) + len("yield_")
of nkDiscardStmt: result = lsub(g, n.sons[0]) + len("discard_")
of nkBreakStmt: result = lsub(g, n.sons[0]) + len("break_")
of nkContinueStmt: result = lsub(g, n.sons[0]) + len("continue_")
of nkRaiseStmt: result = lsub(g, n[0]) + len("raise_")
of nkYieldStmt: result = lsub(g, n[0]) + len("yield_")
of nkDiscardStmt: result = lsub(g, n[0]) + len("discard_")
of nkBreakStmt: result = lsub(g, n[0]) + len("break_")
of nkContinueStmt: result = lsub(g, n[0]) + len("continue_")
of nkPragma: result = lcomma(g, n) + 4
of nkCommentStmt: result = len(n.comment)
of nkCommentStmt: result = n.comment.len
of nkOfBranch: result = lcomma(g, n, 0, - 2) + lsub(g, lastSon(n)) + len("of_:_")
of nkImportAs: result = lsub(g, n.sons[0]) + len("_as_") + lsub(g, n.sons[1])
of nkImportAs: result = lsub(g, n[0]) + len("_as_") + lsub(g, n[1])
of nkElifBranch: result = lsons(g, n) + len("elif_:_")
of nkElse: result = lsub(g, n.sons[0]) + len("else:_")
of nkFinally: result = lsub(g, n.sons[0]) + len("finally:_")
of nkElse: result = lsub(g, n[0]) + len("else:_")
of nkFinally: result = lsub(g, n[0]) + len("finally:_")
of nkGenericParams: result = lcomma(g, n) + 2
of nkFormalParams:
result = lcomma(g, n, 1) + 2
if n.sons[0].kind != nkEmpty: result = result + lsub(g, n.sons[0]) + 2
if n[0].kind != nkEmpty: result = result + lsub(g, n[0]) + 2
of nkExceptBranch:
result = lcomma(g, n, 0, -2) + lsub(g, lastSon(n)) + len("except_:_")
else: result = MaxLineLen + 1
@ -559,8 +556,8 @@ proc hasCom(n: PNode): bool =
case n.kind
of nkEmpty..nkNilLit: discard
else:
for i in 0 ..< len(n):
if hasCom(n.sons[i]): return true
for i in 0..<n.len:
if hasCom(n[i]): return true
proc putWithSpace(g: var TSrcGen, kind: TTokType, s: string) =
put(g, kind, s)
@ -568,16 +565,16 @@ proc putWithSpace(g: var TSrcGen, kind: TTokType, s: string) =
proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
theEnd: int = - 1, separator = tkComma) =
for i in start .. len(n) + theEnd:
var c = i < len(n) + theEnd
var sublen = lsub(g, n.sons[i]) + ord(c)
for i in start..n.len + theEnd:
var c = i < n.len + theEnd
var sublen = lsub(g, n[i]) + ord(c)
if not fits(g, sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
let oldLen = g.tokens.len
gsub(g, n.sons[i])
gsub(g, n[i])
if c:
if g.tokens.len > oldLen:
putWithSpace(g, separator, TokTypeToStr[separator])
if hasCom(n.sons[i]):
if hasCom(n[i]):
gcoms(g)
optNL(g, ind)
@ -603,17 +600,17 @@ proc gsemicolon(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) =
proc gsons(g: var TSrcGen, n: PNode, c: TContext, start: int = 0,
theEnd: int = - 1) =
for i in start .. len(n) + theEnd: gsub(g, n.sons[i], c)
for i in start..n.len + theEnd: gsub(g, n[i], c)
proc gsection(g: var TSrcGen, n: PNode, c: TContext, kind: TTokType,
k: string) =
if len(n) == 0: return # empty var sections are possible
if n.len == 0: return # empty var sections are possible
putWithSpace(g, kind, k)
gcoms(g)
indentNL(g)
for i in 0 ..< len(n):
for i in 0..<n.len:
optNL(g)
gsub(g, n.sons[i], c)
gsub(g, n[i], c)
gcoms(g)
dedent(g)
@ -621,8 +618,8 @@ proc longMode(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): bool =
result = n.comment.len > 0
if not result:
# check further
for i in start .. len(n) + theEnd:
if (lsub(g, n.sons[i]) > MaxLineLen):
for i in start..n.len + theEnd:
if (lsub(g, n[i]) > MaxLineLen):
result = true
break
@ -630,8 +627,7 @@ proc gstmts(g: var TSrcGen, n: PNode, c: TContext, doIndent=true) =
if n.kind == nkEmpty: return
if n.kind in {nkStmtList, nkStmtListExpr, nkStmtListType}:
if doIndent: indentNL(g)
let L = n.len
for i in 0 .. L-1:
for i in 0..<n.len:
if i > 0:
optNL(g, n[i-1], n[i])
else:
@ -659,34 +655,33 @@ proc gcond(g: var TSrcGen, n: PNode) =
proc gif(g: var TSrcGen, n: PNode) =
var c: TContext
gcond(g, n.sons[0].sons[0])
gcond(g, n[0][0])
initContext(c)
putWithSpace(g, tkColon, ":")
if longMode(g, n) or (lsub(g, n.sons[0].sons[1]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[0][1]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[0].sons[1], c)
var length = len(n)
for i in 1 ..< length:
gstmts(g, n[0][1], c)
for i in 1..<n.len:
optNL(g)
gsub(g, n.sons[i], c)
gsub(g, n[i], c)
proc gwhile(g: var TSrcGen, n: PNode) =
var c: TContext
putWithSpace(g, tkWhile, "while")
gcond(g, n.sons[0])
gcond(g, n[0])
putWithSpace(g, tkColon, ":")
initContext(c)
if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[1], c)
gstmts(g, n[1], c)
proc gpattern(g: var TSrcGen, n: PNode) =
var c: TContext
put(g, tkCurlyLe, "{")
initContext(c)
if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n, c)
@ -694,58 +689,55 @@ proc gpattern(g: var TSrcGen, n: PNode) =
proc gpragmaBlock(g: var TSrcGen, n: PNode) =
var c: TContext
gsub(g, n.sons[0])
gsub(g, n[0])
putWithSpace(g, tkColon, ":")
initContext(c)
if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[1], c)
gstmts(g, n[1], c)
proc gtry(g: var TSrcGen, n: PNode) =
var c: TContext
put(g, tkTry, "try")
putWithSpace(g, tkColon, ":")
initContext(c)
if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[0], c)
gstmts(g, n[0], c)
gsons(g, n, c, 1)
proc gfor(g: var TSrcGen, n: PNode) =
var c: TContext
var length = len(n)
putWithSpace(g, tkFor, "for")
initContext(c)
if longMode(g, n) or
(lsub(g, n.sons[length - 1]) + lsub(g, n.sons[length - 2]) + 6 + g.lineLen >
MaxLineLen):
(lsub(g, n[^1]) + lsub(g, n[^2]) + 6 + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcomma(g, n, c, 0, - 3)
put(g, tkSpaces, Space)
putWithSpace(g, tkIn, "in")
gsub(g, n.sons[length - 2], c)
gsub(g, n[^2], c)
putWithSpace(g, tkColon, ":")
gcoms(g)
gstmts(g, n.sons[length - 1], c)
gstmts(g, n[^1], c)
proc gcase(g: var TSrcGen, n: PNode) =
var c: TContext
initContext(c)
var length = len(n)
if length == 0: return
var last = if n.sons[length-1].kind == nkElse: -2 else: -1
if n.len == 0: return
var last = if n[^1].kind == nkElse: -2 else: -1
if longMode(g, n, 0, last): incl(c.flags, rfLongMode)
putWithSpace(g, tkCase, "case")
gcond(g, n.sons[0])
gcond(g, n[0])
gcoms(g)
optNL(g)
gsons(g, n, c, 1, last)
if last == - 2:
initContext(c)
if longMode(g, n.sons[length - 1]): incl(c.flags, rfLongMode)
gsub(g, n.sons[length - 1], c)
if longMode(g, n[^1]): incl(c.flags, rfLongMode)
gsub(g, n[^1], c)
proc genSymSuffix(result: var string, s: PSym) {.inline.} =
if sfGenSym in s.flags:
@ -754,36 +746,36 @@ proc genSymSuffix(result: var string, s: PSym) {.inline.} =
proc gproc(g: var TSrcGen, n: PNode) =
var c: TContext
if n.sons[namePos].kind == nkSym:
let s = n.sons[namePos].sym
if n[namePos].kind == nkSym:
let s = n[namePos].sym
var ret = renderDefinitionName(s)
ret.genSymSuffix(s)
put(g, tkSymbol, ret)
else:
gsub(g, n.sons[namePos])
gsub(g, n[namePos])
if n.sons[patternPos].kind != nkEmpty:
gpattern(g, n.sons[patternPos])
if n[patternPos].kind != nkEmpty:
gpattern(g, n[patternPos])
let oldInGenericParams = g.inGenericParams
g.inGenericParams = true
if renderNoBody in g.flags and n[miscPos].kind != nkEmpty and
n[miscPos][1].kind != nkEmpty:
gsub(g, n[miscPos][1])
else:
gsub(g, n.sons[genericParamsPos])
gsub(g, n[genericParamsPos])
g.inGenericParams = oldInGenericParams
gsub(g, n.sons[paramsPos])
gsub(g, n[paramsPos])
if renderNoPragmas notin g.flags:
gsub(g, n.sons[pragmasPos])
gsub(g, n[pragmasPos])
if renderNoBody notin g.flags:
if n.sons[bodyPos].kind != nkEmpty:
if n[bodyPos].kind != nkEmpty:
put(g, tkSpaces, Space)
putWithSpace(g, tkEquals, "=")
indentNL(g)
gcoms(g)
dedent(g)
initContext(c)
gstmts(g, n.sons[bodyPos], c)
gstmts(g, n[bodyPos], c)
putNL(g)
else:
indentNL(g)
@ -806,33 +798,33 @@ proc gTypeClassTy(g: var TSrcGen, n: PNode) =
proc gblock(g: var TSrcGen, n: PNode) =
var c: TContext
initContext(c)
if n.sons[0].kind != nkEmpty:
if n[0].kind != nkEmpty:
putWithSpace(g, tkBlock, "block")
gsub(g, n.sons[0])
gsub(g, n[0])
else:
put(g, tkBlock, "block")
putWithSpace(g, tkColon, ":")
if longMode(g, n) or (lsub(g, n.sons[1]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[1]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g)
gstmts(g, n.sons[1], c)
gstmts(g, n[1], c)
proc gstaticStmt(g: var TSrcGen, n: PNode) =
var c: TContext
putWithSpace(g, tkStatic, "static")
putWithSpace(g, tkColon, ":")
initContext(c)
if longMode(g, n) or (lsub(g, n.sons[0]) + g.lineLen > MaxLineLen):
if longMode(g, n) or (lsub(g, n[0]) + g.lineLen > MaxLineLen):
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[0], c)
gstmts(g, n[0], c)
proc gasm(g: var TSrcGen, n: PNode) =
putWithSpace(g, tkAsm, "asm")
gsub(g, n.sons[0])
gsub(g, n[0])
gcoms(g)
if n.sons.len > 1:
gsub(g, n.sons[1])
if n.len > 1:
gsub(g, n[1])
proc gident(g: var TSrcGen, n: PNode) =
if g.inGenericParams and n.kind == nkSym:
@ -866,9 +858,9 @@ proc doParamsAux(g: var TSrcGen, params: PNode) =
gsemicolon(g, params, 1)
put(g, tkParRi, ")")
if params.len > 0 and params.sons[0].kind != nkEmpty:
if params.len > 0 and params[0].kind != nkEmpty:
putWithSpace(g, tkOpr, "->")
gsub(g, params.sons[0])
gsub(g, params[0])
proc gsub(g: var TSrcGen; n: PNode; i: int) =
if i < n.len:
@ -970,19 +962,19 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkColon, ":")
gsub(g, n, n.len-1)
else:
if len(n) >= 1: accentedName(g, n[0])
if n.len >= 1: accentedName(g, n[0])
put(g, tkParLe, "(")
gcomma(g, n, 1)
put(g, tkParRi, ")")
of nkCallStrLit:
if n.len > 0: accentedName(g, n[0])
if n.len > 1 and n.sons[1].kind == nkRStrLit:
if n.len > 1 and n[1].kind == nkRStrLit:
put(g, tkRStrLit, '\"' & replace(n[1].strVal, "\"", "\"\"") & '\"')
else:
gsub(g, n, 1)
of nkHiddenStdConv, nkHiddenSubConv, nkHiddenCallConv:
if n.len >= 2:
gsub(g, n.sons[1])
gsub(g, n[1])
else:
put(g, tkSymbol, "(wrong conv)")
of nkCast:
@ -997,7 +989,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkAddr, "addr")
if n.len > 0:
put(g, tkParLe, "(")
gsub(g, n.sons[0])
gsub(g, n[0])
put(g, tkParRi, ")")
of nkStaticExpr:
put(g, tkStatic, "static")
@ -1050,23 +1042,23 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
gcomma(g, n)
put(g, tkParRi, ")")
of nkObjDownConv, nkObjUpConv:
if len(n) >= 1: gsub(g, n.sons[0])
if n.len >= 1: gsub(g, n[0])
put(g, tkParLe, "(")
gcomma(g, n, 1)
put(g, tkParRi, ")")
of nkClosedSymChoice, nkOpenSymChoice:
if renderIds in g.flags:
put(g, tkParLe, "(")
for i in 0 ..< len(n):
for i in 0..<n.len:
if i > 0: put(g, tkOpr, "|")
if n.sons[i].kind == nkSym:
if n[i].kind == nkSym:
let s = n[i].sym
if s.owner != nil:
put g, tkSymbol, n[i].sym.owner.name.s
put g, tkOpr, "."
put g, tkSymbol, n[i].sym.name.s
else:
gsub(g, n.sons[i], c)
gsub(g, n[i], c)
put(g, tkParRi, if n.kind == nkOpenSymChoice: "|...)" else: ")")
else:
gsub(g, n, 0)
@ -1113,20 +1105,19 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
of nkDo:
putWithSpace(g, tkDo, "do")
if paramsPos < n.len:
doParamsAux(g, n.sons[paramsPos])
doParamsAux(g, n[paramsPos])
gsub(g, n, pragmasPos)
put(g, tkColon, ":")
gsub(g, n, bodyPos)
of nkConstDef, nkIdentDefs:
gcomma(g, n, 0, -3)
var L = len(n)
if L >= 2 and n.sons[L - 2].kind != nkEmpty:
if n.len >= 2 and n[^2].kind != nkEmpty:
putWithSpace(g, tkColon, ":")
gsub(g, n, L - 2)
if L >= 1 and n.sons[L - 1].kind != nkEmpty:
gsub(g, n, n.len - 2)
if n.len >= 1 and n[^1].kind != nkEmpty:
put(g, tkSpaces, Space)
putWithSpace(g, tkEquals, "=")
gsub(g, n.sons[L - 1], c)
gsub(g, n[^1], c)
of nkVarTuple:
put(g, tkParLe, "(")
gcomma(g, n, 0, -3)
@ -1142,7 +1133,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
infixArgument(g, n, 1)
put(g, tkSpaces, Space)
gsub(g, n, 0) # binary operator
if n.len == 3 and not fits(g, lsub(g, n.sons[2]) + lsub(g, n.sons[0]) + 1):
if n.len == 3 and not fits(g, lsub(g, n[2]) + lsub(g, n[0]) + 1):
optNL(g, g.indent + longIndentWid)
else:
put(g, tkSpaces, Space)
@ -1162,10 +1153,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkSpaces, Space)
if nNext.kind == nkInfix:
put(g, tkParLe, "(")
gsub(g, n.sons[1])
gsub(g, n[1])
put(g, tkParRi, ")")
else:
gsub(g, n.sons[1])
gsub(g, n[1])
of nkPostfix:
gsub(g, n, 1)
gsub(g, n, 0)
@ -1178,16 +1169,16 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkOpr, "[]")
of nkAccQuoted:
put(g, tkAccent, "`")
if n.len > 0: gsub(g, n.sons[0])
for i in 1 ..< n.len:
if n.len > 0: gsub(g, n[0])
for i in 1..<n.len:
put(g, tkSpaces, Space)
gsub(g, n.sons[i])
gsub(g, n[i])
put(g, tkAccent, "`")
of nkIfExpr:
putWithSpace(g, tkIf, "if")
if n.len > 0: gcond(g, n.sons[0].sons[0])
if n.len > 0: gcond(g, n[0][0])
putWithSpace(g, tkColon, ":")
if n.len > 0: gsub(g, n.sons[0], 1)
if n.len > 0: gsub(g, n[0], 1)
gsons(g, n, emptyContext, 1)
of nkElifExpr:
putWithSpace(g, tkElif, " elif")
@ -1201,30 +1192,30 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
of nkTypeOfExpr:
put(g, tkType, "typeof")
put(g, tkParLe, "(")
if n.len > 0: gsub(g, n.sons[0])
if n.len > 0: gsub(g, n[0])
put(g, tkParRi, ")")
of nkRefTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkRef, "ref")
gsub(g, n.sons[0])
gsub(g, n[0])
else:
put(g, tkRef, "ref")
of nkPtrTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkPtr, "ptr")
gsub(g, n.sons[0])
gsub(g, n[0])
else:
put(g, tkPtr, "ptr")
of nkVarTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkVar, "var")
gsub(g, n.sons[0])
gsub(g, n[0])
else:
put(g, tkVar, "var")
of nkDistinctTy:
if n.len > 0:
putWithSpace(g, tkDistinct, "distinct")
gsub(g, n.sons[0])
gsub(g, n[0])
if n.len > 1:
if n[1].kind == nkWith:
putWithSpace(g, tkSymbol, " with")
@ -1234,32 +1225,32 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
else:
put(g, tkDistinct, "distinct")
of nkTypeDef:
if n.sons[0].kind == nkPragmaExpr:
if n[0].kind == nkPragmaExpr:
# generate pragma after generic
gsub(g, n.sons[0], 0)
gsub(g, n[0], 0)
gsub(g, n, 1)
gsub(g, n.sons[0], 1)
gsub(g, n[0], 1)
else:
gsub(g, n, 0)
gsub(g, n, 1)
put(g, tkSpaces, Space)
if n.len > 2 and n.sons[2].kind != nkEmpty:
if n.len > 2 and n[2].kind != nkEmpty:
putWithSpace(g, tkEquals, "=")
gsub(g, n.sons[2])
gsub(g, n[2])
of nkObjectTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkObject, "object")
gsub(g, n.sons[0])
gsub(g, n.sons[1])
gsub(g, n[0])
gsub(g, n[1])
gcoms(g)
gsub(g, n.sons[2])
gsub(g, n[2])
else:
put(g, tkObject, "object")
of nkRecList:
indentNL(g)
for i in 0 ..< len(n):
for i in 0..<n.len:
optNL(g)
gsub(g, n.sons[i], c)
gsub(g, n[i], c)
gcoms(g)
dedent(g)
putNL(g)
@ -1267,14 +1258,14 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
putWithSpace(g, tkOf, "of")
gsub(g, n, 0)
of nkProcTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkProc, "proc")
gsub(g, n, 0)
gsub(g, n, 1)
else:
put(g, tkProc, "proc")
of nkIteratorTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkIterator, "iterator")
gsub(g, n, 0)
gsub(g, n, 1)
@ -1284,12 +1275,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkStatic, "static")
put(g, tkBracketLe, "[")
if n.len > 0:
gsub(g, n.sons[0])
gsub(g, n[0])
put(g, tkBracketRi, "]")
of nkEnumTy:
if len(n) > 0:
if n.len > 0:
putWithSpace(g, tkEnum, "enum")
gsub(g, n.sons[0])
gsub(g, n[0])
gcoms(g)
indentNL(g)
gcommaAux(g, n, g.indent, 1)
@ -1345,21 +1336,20 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
incl(a.flags, rfInConstExpr)
gsection(g, n, a, tkConst, "const")
of nkVarSection, nkLetSection, nkUsingStmt:
var L = len(n)
if L == 0: return
if n.len == 0: return
if n.kind == nkVarSection: putWithSpace(g, tkVar, "var")
elif n.kind == nkLetSection: putWithSpace(g, tkLet, "let")
else: putWithSpace(g, tkUsing, "using")
if L > 1:
if n.len > 1:
gcoms(g)
indentNL(g)
for i in 0 ..< L:
for i in 0..<n.len:
optNL(g)
gsub(g, n.sons[i])
gsub(g, n[i])
gcoms(g)
dedent(g)
else:
gsub(g, n.sons[0])
gsub(g, n[0])
of nkReturnStmt:
putWithSpace(g, tkReturn, "return")
if n.len > 0 and n[0].kind == nkAsgn:
@ -1454,13 +1444,13 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
gsub(g, n, 0)
putWithSpace(g, tkColon, ":")
gcoms(g)
gstmts(g, n.sons[1], c)
gstmts(g, n[1], c)
of nkElse:
optNL(g)
put(g, tkElse, "else")
putWithSpace(g, tkColon, ":")
gcoms(g)
gstmts(g, n.sons[0], c)
gstmts(g, n[0], c)
of nkFinally, nkDefer:
optNL(g)
if n.kind == nkFinally:
@ -1469,7 +1459,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkDefer, "defer")
putWithSpace(g, tkColon, ":")
gcoms(g)
gstmts(g, n.sons[0], c)
gstmts(g, n[0], c)
of nkExceptBranch:
optNL(g)
if n.len != 1:
@ -1495,9 +1485,9 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkParLe, "(")
gsemicolon(g, n, 1)
put(g, tkParRi, ")")
if n.len > 0 and n.sons[0].kind != nkEmpty:
if n.len > 0 and n[0].kind != nkEmpty:
putWithSpace(g, tkColon, ":")
gsub(g, n.sons[0])
gsub(g, n[0])
of nkTupleTy:
put(g, tkTuple, "tuple")
put(g, tkBracketLe, "[")
@ -1558,10 +1548,10 @@ proc renderModule*(n: PNode, infile, outfile: string,
g: TSrcGen
initSrcGen(g, renderFlags, conf)
g.fid = fid
for i in 0 ..< len(n):
gsub(g, n.sons[i])
for i in 0..<n.len:
gsub(g, n[i])
optNL(g)
case n.sons[i].kind
case n[i].kind
of nkTypeSection, nkConstSection, nkVarSection, nkLetSection,
nkCommentStmt: putNL(g)
else: discard
@ -1577,9 +1567,9 @@ proc initTokRender*(r: var TSrcGen, n: PNode, renderFlags: TRenderFlags = {}) =
gsub(r, n)
proc getNextTok*(r: var TSrcGen, kind: var TTokType, literal: var string) =
if r.idx < len(r.tokens):
if r.idx < r.tokens.len:
kind = r.tokens[r.idx].kind
var length = r.tokens[r.idx].length.int
let length = r.tokens[r.idx].length.int
literal = substr(r.buf, r.pos, r.pos + length - 1)
inc(r.pos, length)
inc(r.idx)
@ -1587,7 +1577,7 @@ proc getNextTok*(r: var TSrcGen, kind: var TTokType, literal: var string) =
kind = tkEof
proc getTokSym*(r: TSrcGen): PSym =
if r.idx > 0 and r.idx <= len(r.tokens):
if r.idx > 0 and r.idx <= r.tokens.len:
result = r.tokens[r.idx-1].sym
else:
result = nil