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

@ -27,19 +27,18 @@ proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode =
of nkIdent, nkSym:
result = n
let ident = considerQuotedIdent(c.c, n)
var L = len(forLoop)
if c.replaceByFieldName:
if ident.id == considerQuotedIdent(c.c, forLoop[0]).id:
let fieldName = if c.tupleType.isNil: c.field.name.s
elif c.tupleType.n.isNil: "Field" & $c.tupleIndex
else: c.tupleType.n.sons[c.tupleIndex].sym.name.s
else: c.tupleType.n[c.tupleIndex].sym.name.s
result = newStrNode(nkStrLit, fieldName)
return
# other fields:
for i in ord(c.replaceByFieldName)..L-3:
for i in ord(c.replaceByFieldName)..<forLoop.len-2:
if ident.id == considerQuotedIdent(c.c, forLoop[i]).id:
var call = forLoop.sons[L-2]
var tupl = call.sons[i+1-ord(c.replaceByFieldName)]
var call = forLoop[^2]
var tupl = call[i+1-ord(c.replaceByFieldName)]
if c.field.isNil:
result = newNodeI(nkBracketExpr, n.info)
result.add(tupl)
@ -54,9 +53,9 @@ proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode =
localError(c.c.config, n.info,
"'continue' not supported in a 'fields' loop")
result = copyNode(n)
newSons(result, len(n))
for i in 0 ..< len(n):
result.sons[i] = instFieldLoopBody(c, n.sons[i], forLoop)
newSons(result, n.len)
for i in 0..<n.len:
result[i] = instFieldLoopBody(c, n[i], forLoop)
type
TFieldsCtx = object
@ -78,8 +77,7 @@ proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
closeScope(c.c)
of nkNilLit: discard
of nkRecCase:
let L = forLoop.len
let call = forLoop.sons[L-2]
let call = forLoop[^2]
if call.len > 2:
localError(c.c.config, forLoop.info,
"parallel 'fields' iterator does not work for 'case' objects")
@ -90,15 +88,14 @@ proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
var caseStmt = newNodeI(nkCaseStmt, forLoop.info)
# generate selector:
var access = newNodeI(nkDotExpr, forLoop.info, 2)
access.sons[0] = call.sons[1]
access.sons[1] = newSymNode(typ.sons[0].sym, forLoop.info)
access[0] = call[1]
access[1] = newSymNode(typ[0].sym, forLoop.info)
caseStmt.add(semExprWithType(c.c, access))
# copy the branches over, but replace the fields with the for loop body:
for i in 1 ..< typ.len:
for i in 1..<typ.len:
var branch = copyTree(typ[i])
let L = branch.len
branch.sons[L-1] = newNodeI(nkStmtList, forLoop.info)
semForObjectFields(c, typ[i].lastSon, forLoop, branch[L-1])
branch[^1] = newNodeI(nkStmtList, forLoop.info)
semForObjectFields(c, typ[i].lastSon, forLoop, branch[^1])
caseStmt.add(branch)
father.add(caseStmt)
of nkRecList:
@ -116,30 +113,29 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), getCurrOwner(c), n.info)
trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
result.sons[0] = newSymNode(trueSymbol, n.info)
result[0] = newSymNode(trueSymbol, n.info)
var stmts = newNodeI(nkStmtList, n.info)
result.sons[1] = stmts
result[1] = stmts
var length = len(n)
var call = n.sons[length-2]
if length-2 != len(call)-1 + ord(m==mFieldPairs):
var call = n[^2]
if n.len-2 != call.len-1 + ord(m==mFieldPairs):
localError(c.config, n.info, errWrongNumberOfVariables)
return result
const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
var tupleTypeA = skipTypes(call.sons[1].typ, skippedTypesForFields)
var tupleTypeA = skipTypes(call[1].typ, skippedTypesForFields)
if tupleTypeA.kind notin {tyTuple, tyObject}:
localError(c.config, n.info, errGenerated, "no object or tuple type")
return result
for i in 1..call.len-1:
var tupleTypeB = skipTypes(call.sons[i].typ, skippedTypesForFields)
for i in 1..<call.len:
var tupleTypeB = skipTypes(call[i].typ, skippedTypesForFields)
if not sameType(tupleTypeA, tupleTypeB):
typeMismatch(c.config, call.sons[i].info, tupleTypeA, tupleTypeB)
typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB)
inc(c.p.nestedLoopCounter)
if tupleTypeA.kind == tyTuple:
var loopBody = n.sons[length-1]
for i in 0..len(tupleTypeA)-1:
var loopBody = n[^1]
for i in 0..<tupleTypeA.len:
openScope(c)
var fc: TFieldInstCtx
fc.tupleType = tupleTypeA
@ -158,8 +154,8 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
var t = tupleTypeA
while t.kind == tyObject:
semForObjectFields(fc, t.n, n, stmts)
if t.sons[0] == nil: break
t = skipTypes(t.sons[0], skipPtrs)
if t[0] == nil: break
t = skipTypes(t[0], skipPtrs)
dec(c.p.nestedLoopCounter)
# for TR macros this 'while true: ...; break' loop is pretty bad, so
# we avoid it now if we can: