Skip to content

Commit 7e747d1

Browse files
ClyybberAraq
authored andcommitted
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
1 parent b662842 commit 7e747d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+6116
-6255
lines changed

compiler/aliases.nim

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ proc isPartOfAux(n: PNode, b: PType, marker: var IntSet): TAnalysisResult =
2222
result = arNo
2323
case n.kind
2424
of nkRecList:
25-
for i in 0 ..< len(n):
26-
result = isPartOfAux(n.sons[i], b, marker)
25+
for i in 0..<n.len:
26+
result = isPartOfAux(n[i], b, marker)
2727
if result == arYes: return
2828
of nkRecCase:
29-
assert(n.sons[0].kind == nkSym)
30-
result = isPartOfAux(n.sons[0], b, marker)
29+
assert(n[0].kind == nkSym)
30+
result = isPartOfAux(n[0], b, marker)
3131
if result == arYes: return
32-
for i in 1 ..< len(n):
33-
case n.sons[i].kind
32+
for i in 1..<n.len:
33+
case n[i].kind
3434
of nkOfBranch, nkElse:
35-
result = isPartOfAux(lastSon(n.sons[i]), b, marker)
35+
result = isPartOfAux(lastSon(n[i]), b, marker)
3636
if result == arYes: return
3737
else: discard "isPartOfAux(record case branch)"
3838
of nkSym:
@@ -46,14 +46,14 @@ proc isPartOfAux(a, b: PType, marker: var IntSet): TAnalysisResult =
4646
if compareTypes(a, b, dcEqIgnoreDistinct): return arYes
4747
case a.kind
4848
of tyObject:
49-
if a.sons[0] != nil:
50-
result = isPartOfAux(a.sons[0].skipTypes(skipPtrs), b, marker)
49+
if a[0] != nil:
50+
result = isPartOfAux(a[0].skipTypes(skipPtrs), b, marker)
5151
if result == arNo: result = isPartOfAux(a.n, b, marker)
5252
of tyGenericInst, tyDistinct, tyAlias, tySink:
5353
result = isPartOfAux(lastSon(a), b, marker)
5454
of tyArray, tySet, tyTuple:
55-
for i in 0 ..< len(a):
56-
result = isPartOfAux(a.sons[i], b, marker)
55+
for i in 0..<a.len:
56+
result = isPartOfAux(a[i], b, marker)
5757
if result == arYes: return
5858
else: discard
5959

@@ -108,7 +108,7 @@ proc isPartOf*(a, b: PNode): TAnalysisResult =
108108
result = arMaybe
109109
of nkBracketExpr:
110110
result = isPartOf(a[0], b[0])
111-
if len(a) >= 2 and len(b) >= 2:
111+
if a.len >= 2 and b.len >= 2:
112112
# array accesses:
113113
if result == arYes and isDeepConstExpr(a[1]) and isDeepConstExpr(b[1]):
114114
# we know it's the same array and we have 2 constant indexes;
@@ -181,14 +181,14 @@ proc isPartOf*(a, b: PNode): TAnalysisResult =
181181
else: discard
182182
of nkObjConstr:
183183
result = arNo
184-
for i in 1 ..< b.len:
184+
for i in 1..<b.len:
185185
let res = isPartOf(a, b[i][1])
186186
if res != arNo:
187187
result = res
188188
if res == arYes: break
189189
of nkCallKinds:
190190
result = arNo
191-
for i in 1 ..< b.len:
191+
for i in 1..<b.len:
192192
let res = isPartOf(a, b[i])
193193
if res != arNo:
194194
result = res

compiler/ast.nim

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,27 +1025,27 @@ type Indexable = PNode | PType
10251025

10261026
proc len*(n: Indexable): int {.inline.} =
10271027
when defined(nimNoNilSeqs):
1028-
result = len(n.sons)
1028+
result = n.sons.len
10291029
else:
10301030
if isNil(n.sons): result = 0
1031-
else: result = len(n.sons)
1031+
else: result = n.sons.len
10321032

10331033
proc safeLen*(n: PNode): int {.inline.} =
10341034
## works even for leaves.
10351035
if n.kind in {nkNone..nkNilLit}: result = 0
1036-
else: result = len(n)
1036+
else: result = n.len
10371037

10381038
proc safeArrLen*(n: PNode): int {.inline.} =
10391039
## works for array-like objects (strings passed as openArray in VM).
1040-
if n.kind in {nkStrLit..nkTripleStrLit}:result = len(n.strVal)
1040+
if n.kind in {nkStrLit..nkTripleStrLit}:result = n.strVal.len
10411041
elif n.kind in {nkNone..nkFloat128Lit}: result = 0
1042-
else: result = len(n)
1042+
else: result = n.len
10431043

1044-
proc add*(father, son: PNode) =
1044+
proc add*(father, son: Indexable) =
10451045
assert son != nil
10461046
when not defined(nimNoNilSeqs):
10471047
if isNil(father.sons): father.sons = @[]
1048-
add(father.sons, son)
1048+
father.sons.add(son)
10491049

10501050
template `[]`*(n: Indexable, i: int): Indexable = n.sons[i]
10511051
template `[]=`*(n: Indexable, i: int; x: Indexable) = n.sons[i] = x
@@ -1144,18 +1144,18 @@ const # for all kind of hash tables:
11441144

11451145
proc copyStrTable*(dest: var TStrTable, src: TStrTable) =
11461146
dest.counter = src.counter
1147-
setLen(dest.data, len(src.data))
1148-
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
1147+
setLen(dest.data, src.data.len)
1148+
for i in 0..high(src.data): dest.data[i] = src.data[i]
11491149

11501150
proc copyIdTable*(dest: var TIdTable, src: TIdTable) =
11511151
dest.counter = src.counter
1152-
newSeq(dest.data, len(src.data))
1153-
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
1152+
newSeq(dest.data, src.data.len)
1153+
for i in 0..high(src.data): dest.data[i] = src.data[i]
11541154

11551155
proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) =
11561156
dest.counter = src.counter
1157-
setLen(dest.data, len(src.data))
1158-
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
1157+
setLen(dest.data, src.data.len)
1158+
for i in 0..high(src.data): dest.data[i] = src.data[i]
11591159

11601160
proc discardSons*(father: PNode) =
11611161
when defined(nimNoNilSeqs):
@@ -1286,12 +1286,6 @@ proc newStrNode*(strVal: string; info: TLineInfo): PNode =
12861286
result = newNodeI(nkStrLit, info)
12871287
result.strVal = strVal
12881288

1289-
proc addSon*(father, son: PNode) =
1290-
assert son != nil
1291-
when not defined(nimNoNilSeqs):
1292-
if isNil(father.sons): father.sons = @[]
1293-
add(father.sons, son)
1294-
12951289
proc newProcNode*(kind: TNodeKind, info: TLineInfo, body: PNode,
12961290
params,
12971291
name, pattern, genericParams,
@@ -1366,8 +1360,8 @@ proc assignType*(dest, src: PType) =
13661360
mergeLoc(dest.sym.loc, src.sym.loc)
13671361
else:
13681362
dest.sym = src.sym
1369-
newSons(dest, len(src))
1370-
for i in 0 ..< len(src): dest.sons[i] = src.sons[i]
1363+
newSons(dest, src.len)
1364+
for i in 0..<src.len: dest[i] = src[i]
13711365

13721366
proc copyType*(t: PType, owner: PSym, keepId: bool): PType =
13731367
result = newType(t.kind, owner)
@@ -1503,27 +1497,26 @@ proc propagateToOwner*(owner, elem: PType) =
15031497
proc rawAddSon*(father, son: PType) =
15041498
when not defined(nimNoNilSeqs):
15051499
if isNil(father.sons): father.sons = @[]
1506-
add(father.sons, son)
1500+
father.sons.add(son)
15071501
if not son.isNil: propagateToOwner(father, son)
15081502

15091503
proc rawAddSonNoPropagationOfTypeFlags*(father, son: PType) =
15101504
when not defined(nimNoNilSeqs):
15111505
if isNil(father.sons): father.sons = @[]
1512-
add(father.sons, son)
1506+
father.sons.add(son)
15131507

15141508
proc addSonNilAllowed*(father, son: PNode) =
15151509
when not defined(nimNoNilSeqs):
15161510
if isNil(father.sons): father.sons = @[]
1517-
add(father.sons, son)
1511+
father.sons.add(son)
15181512

15191513
proc delSon*(father: PNode, idx: int) =
15201514
when defined(nimNoNilSeqs):
15211515
if father.len == 0: return
15221516
else:
15231517
if isNil(father.sons): return
1524-
var length = len(father)
1525-
for i in idx .. length - 2: father.sons[i] = father.sons[i + 1]
1526-
setLen(father.sons, length - 1)
1518+
for i in idx..<father.len - 1: father[i] = father[i + 1]
1519+
father.sons.setLen(father.len - 1)
15271520

15281521
proc copyNode*(src: PNode): PNode =
15291522
# does not copy its sons!
@@ -1562,7 +1555,7 @@ proc shallowCopy*(src: PNode): PNode =
15621555
of nkSym: result.sym = src.sym
15631556
of nkIdent: result.ident = src.ident
15641557
of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
1565-
else: newSeq(result.sons, len(src))
1558+
else: newSeq(result.sons, src.len)
15661559

15671560
proc copyTree*(src: PNode): PNode =
15681561
# copy a whole syntax tree; performs deep copying
@@ -1583,21 +1576,21 @@ proc copyTree*(src: PNode): PNode =
15831576
of nkIdent: result.ident = src.ident
15841577
of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
15851578
else:
1586-
newSeq(result.sons, len(src))
1587-
for i in 0 ..< len(src):
1588-
result.sons[i] = copyTree(src.sons[i])
1579+
newSeq(result.sons, src.len)
1580+
for i in 0..<src.len:
1581+
result[i] = copyTree(src[i])
15891582

15901583
proc hasSonWith*(n: PNode, kind: TNodeKind): bool =
1591-
for i in 0 ..< len(n):
1592-
if n.sons[i].kind == kind:
1584+
for i in 0..<n.len:
1585+
if n[i].kind == kind:
15931586
return true
15941587
result = false
15951588

15961589
proc hasNilSon*(n: PNode): bool =
1597-
for i in 0 ..< safeLen(n):
1598-
if n.sons[i] == nil:
1590+
for i in 0..<n.safeLen:
1591+
if n[i] == nil:
15991592
return true
1600-
elif hasNilSon(n.sons[i]):
1593+
elif hasNilSon(n[i]):
16011594
return true
16021595
result = false
16031596

@@ -1606,15 +1599,15 @@ proc containsNode*(n: PNode, kinds: TNodeKinds): bool =
16061599
case n.kind
16071600
of nkEmpty..nkNilLit: result = n.kind in kinds
16081601
else:
1609-
for i in 0 ..< len(n):
1610-
if n.kind in kinds or containsNode(n.sons[i], kinds): return true
1602+
for i in 0..<n.len:
1603+
if n.kind in kinds or containsNode(n[i], kinds): return true
16111604

16121605
proc hasSubnodeWith*(n: PNode, kind: TNodeKind): bool =
16131606
case n.kind
16141607
of nkEmpty..nkNilLit: result = n.kind == kind
16151608
else:
1616-
for i in 0 ..< len(n):
1617-
if (n.sons[i].kind == kind) or hasSubnodeWith(n.sons[i], kind):
1609+
for i in 0..<n.len:
1610+
if (n[i].kind == kind) or hasSubnodeWith(n[i], kind):
16181611
return true
16191612
result = false
16201613

@@ -1710,19 +1703,19 @@ proc requiredParams*(s: PSym): int =
17101703
# Returns the number of required params (without default values)
17111704
# XXX: Perhaps we can store this in the `offset` field of the
17121705
# symbol instead?
1713-
for i in 1 ..< s.typ.len:
1706+
for i in 1..<s.typ.len:
17141707
if s.typ.n[i].sym.ast != nil:
17151708
return i - 1
17161709
return s.typ.len - 1
17171710

17181711
proc hasPattern*(s: PSym): bool {.inline.} =
1719-
result = isRoutine(s) and s.ast.sons[patternPos].kind != nkEmpty
1712+
result = isRoutine(s) and s.ast[patternPos].kind != nkEmpty
17201713

17211714
iterator items*(n: PNode): PNode =
1722-
for i in 0..<n.safeLen: yield n.sons[i]
1715+
for i in 0..<n.safeLen: yield n[i]
17231716

17241717
iterator pairs*(n: PNode): tuple[i: int, n: PNode] =
1725-
for i in 0..<n.safeLen: yield (i, n.sons[i])
1718+
for i in 0..<n.safeLen: yield (i, n[i])
17261719

17271720
proc isAtom*(n: PNode): bool {.inline.} =
17281721
result = n.kind >= nkNone and n.kind <= nkNilLit
@@ -1740,7 +1733,7 @@ proc makeStmtList*(n: PNode): PNode =
17401733

17411734
proc skipStmtList*(n: PNode): PNode =
17421735
if n.kind in {nkStmtList, nkStmtListExpr}:
1743-
for i in 0 .. n.len-2:
1736+
for i in 0..<n.len-1:
17441737
if n[i].kind notin {nkEmpty, nkCommentStmt}: return n
17451738
result = n.lastSon
17461739
else:
@@ -1797,7 +1790,7 @@ when false:
17971790
proc containsNil*(n: PNode): bool =
17981791
# only for debugging
17991792
if n.isNil: return true
1800-
for i in 0 ..< n.safeLen:
1793+
for i in 0..<n.safeLen:
18011794
if n[i].containsNil: return true
18021795

18031796
template hasDestructor*(t: PType): bool = {tfHasAsgn, tfHasOwned} * t.flags != {}
@@ -1831,11 +1824,11 @@ proc newProcType*(info: TLineInfo; owner: PSym): PType =
18311824
# result.n[0] used to be `nkType`, but now it's `nkEffectList` because
18321825
# the effects are now stored in there too ... this is a bit hacky, but as
18331826
# usual we desperately try to save memory:
1834-
addSon(result.n, newNodeI(nkEffectList, info))
1827+
result.n.add newNodeI(nkEffectList, info)
18351828

18361829
proc addParam*(procType: PType; param: PSym) =
18371830
param.position = procType.len-1
1838-
addSon(procType.n, newSymNode(param))
1831+
procType.n.add newSymNode(param)
18391832
rawAddSon(procType, param.typ)
18401833

18411834
template destructor*(t: PType): PSym = t.attachedOps[attachedDestructor]

0 commit comments

Comments
 (0)