Skip to content

Commit

Permalink
Cosmetic compiler cleanup (#12718)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Clyybber authored and Araq committed Nov 28, 2019
1 parent b662842 commit 7e747d1
Show file tree
Hide file tree
Showing 109 changed files with 6,116 additions and 6,255 deletions.
28 changes: 14 additions & 14 deletions compiler/aliases.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n.len:
result = isPartOfAux(n[i], b, marker)
if result == arYes: return
of nkRecCase:
assert(n.sons[0].kind == nkSym)
result = isPartOfAux(n.sons[0], b, marker)
assert(n[0].kind == nkSym)
result = isPartOfAux(n[0], b, marker)
if result == arYes: return
for i in 1 ..< len(n):
case n.sons[i].kind
for i in 1..<n.len:
case n[i].kind
of nkOfBranch, nkElse:
result = isPartOfAux(lastSon(n.sons[i]), b, marker)
result = isPartOfAux(lastSon(n[i]), b, marker)
if result == arYes: return
else: discard "isPartOfAux(record case branch)"
of nkSym:
Expand All @@ -46,14 +46,14 @@ proc isPartOfAux(a, b: PType, marker: var IntSet): TAnalysisResult =
if compareTypes(a, b, dcEqIgnoreDistinct): return arYes
case a.kind
of tyObject:
if a.sons[0] != nil:
result = isPartOfAux(a.sons[0].skipTypes(skipPtrs), b, marker)
if a[0] != nil:
result = isPartOfAux(a[0].skipTypes(skipPtrs), b, marker)
if result == arNo: result = isPartOfAux(a.n, b, marker)
of tyGenericInst, tyDistinct, tyAlias, tySink:
result = isPartOfAux(lastSon(a), b, marker)
of tyArray, tySet, tyTuple:
for i in 0 ..< len(a):
result = isPartOfAux(a.sons[i], b, marker)
for i in 0..<a.len:
result = isPartOfAux(a[i], b, marker)
if result == arYes: return
else: discard

Expand Down Expand Up @@ -108,7 +108,7 @@ proc isPartOf*(a, b: PNode): TAnalysisResult =
result = arMaybe
of nkBracketExpr:
result = isPartOf(a[0], b[0])
if len(a) >= 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;
Expand Down Expand Up @@ -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..<b.len:
let res = isPartOf(a, b[i][1])
if res != arNo:
result = res
if res == arYes: break
of nkCallKinds:
result = arNo
for i in 1 ..< b.len:
for i in 1..<b.len:
let res = isPartOf(a, b[i])
if res != arNo:
result = res
Expand Down
89 changes: 41 additions & 48 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1025,27 +1025,27 @@ type Indexable = PNode | PType

proc len*(n: Indexable): int {.inline.} =
when defined(nimNoNilSeqs):
result = len(n.sons)
result = n.sons.len
else:
if isNil(n.sons): result = 0
else: result = len(n.sons)
else: result = n.sons.len

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

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

proc add*(father, son: PNode) =
proc add*(father, son: Indexable) =
assert son != nil
when not defined(nimNoNilSeqs):
if isNil(father.sons): father.sons = @[]
add(father.sons, son)
father.sons.add(son)

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

proc copyStrTable*(dest: var TStrTable, src: TStrTable) =
dest.counter = src.counter
setLen(dest.data, len(src.data))
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
setLen(dest.data, src.data.len)
for i in 0..high(src.data): dest.data[i] = src.data[i]

proc copyIdTable*(dest: var TIdTable, src: TIdTable) =
dest.counter = src.counter
newSeq(dest.data, len(src.data))
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
newSeq(dest.data, src.data.len)
for i in 0..high(src.data): dest.data[i] = src.data[i]

proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) =
dest.counter = src.counter
setLen(dest.data, len(src.data))
for i in 0 .. high(src.data): dest.data[i] = src.data[i]
setLen(dest.data, src.data.len)
for i in 0..high(src.data): dest.data[i] = src.data[i]

proc discardSons*(father: PNode) =
when defined(nimNoNilSeqs):
Expand Down Expand Up @@ -1286,12 +1286,6 @@ proc newStrNode*(strVal: string; info: TLineInfo): PNode =
result = newNodeI(nkStrLit, info)
result.strVal = strVal

proc addSon*(father, son: PNode) =
assert son != nil
when not defined(nimNoNilSeqs):
if isNil(father.sons): father.sons = @[]
add(father.sons, son)

proc newProcNode*(kind: TNodeKind, info: TLineInfo, body: PNode,
params,
name, pattern, genericParams,
Expand Down Expand Up @@ -1366,8 +1360,8 @@ proc assignType*(dest, src: PType) =
mergeLoc(dest.sym.loc, src.sym.loc)
else:
dest.sym = src.sym
newSons(dest, len(src))
for i in 0 ..< len(src): dest.sons[i] = src.sons[i]
newSons(dest, src.len)
for i in 0..<src.len: dest[i] = src[i]

proc copyType*(t: PType, owner: PSym, keepId: bool): PType =
result = newType(t.kind, owner)
Expand Down Expand Up @@ -1503,27 +1497,26 @@ proc propagateToOwner*(owner, elem: PType) =
proc rawAddSon*(father, son: PType) =
when not defined(nimNoNilSeqs):
if isNil(father.sons): father.sons = @[]
add(father.sons, son)
father.sons.add(son)
if not son.isNil: propagateToOwner(father, son)

proc rawAddSonNoPropagationOfTypeFlags*(father, son: PType) =
when not defined(nimNoNilSeqs):
if isNil(father.sons): father.sons = @[]
add(father.sons, son)
father.sons.add(son)

proc addSonNilAllowed*(father, son: PNode) =
when not defined(nimNoNilSeqs):
if isNil(father.sons): father.sons = @[]
add(father.sons, son)
father.sons.add(son)

proc delSon*(father: PNode, idx: int) =
when defined(nimNoNilSeqs):
if father.len == 0: return
else:
if isNil(father.sons): return
var length = len(father)
for i in idx .. length - 2: father.sons[i] = father.sons[i + 1]
setLen(father.sons, length - 1)
for i in idx..<father.len - 1: father[i] = father[i + 1]
father.sons.setLen(father.len - 1)

proc copyNode*(src: PNode): PNode =
# does not copy its sons!
Expand Down Expand Up @@ -1562,7 +1555,7 @@ proc shallowCopy*(src: PNode): PNode =
of nkSym: result.sym = src.sym
of nkIdent: result.ident = src.ident
of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
else: newSeq(result.sons, len(src))
else: newSeq(result.sons, src.len)

proc copyTree*(src: PNode): PNode =
# copy a whole syntax tree; performs deep copying
Expand All @@ -1583,21 +1576,21 @@ proc copyTree*(src: PNode): PNode =
of nkIdent: result.ident = src.ident
of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
else:
newSeq(result.sons, len(src))
for i in 0 ..< len(src):
result.sons[i] = copyTree(src.sons[i])
newSeq(result.sons, src.len)
for i in 0..<src.len:
result[i] = copyTree(src[i])

proc hasSonWith*(n: PNode, kind: TNodeKind): bool =
for i in 0 ..< len(n):
if n.sons[i].kind == kind:
for i in 0..<n.len:
if n[i].kind == kind:
return true
result = false

proc hasNilSon*(n: PNode): bool =
for i in 0 ..< safeLen(n):
if n.sons[i] == nil:
for i in 0..<n.safeLen:
if n[i] == nil:
return true
elif hasNilSon(n.sons[i]):
elif hasNilSon(n[i]):
return true
result = false

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

proc hasSubnodeWith*(n: PNode, kind: TNodeKind): bool =
case n.kind
of nkEmpty..nkNilLit: result = n.kind == kind
else:
for i in 0 ..< len(n):
if (n.sons[i].kind == kind) or hasSubnodeWith(n.sons[i], kind):
for i in 0..<n.len:
if (n[i].kind == kind) or hasSubnodeWith(n[i], kind):
return true
result = false

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

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

iterator items*(n: PNode): PNode =
for i in 0..<n.safeLen: yield n.sons[i]
for i in 0..<n.safeLen: yield n[i]

iterator pairs*(n: PNode): tuple[i: int, n: PNode] =
for i in 0..<n.safeLen: yield (i, n.sons[i])
for i in 0..<n.safeLen: yield (i, n[i])

proc isAtom*(n: PNode): bool {.inline.} =
result = n.kind >= nkNone and n.kind <= nkNilLit
Expand All @@ -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..<n.len-1:
if n[i].kind notin {nkEmpty, nkCommentStmt}: return n
result = n.lastSon
else:
Expand Down Expand Up @@ -1797,7 +1790,7 @@ when false:
proc containsNil*(n: PNode): bool =
# only for debugging
if n.isNil: return true
for i in 0 ..< n.safeLen:
for i in 0..<n.safeLen:
if n[i].containsNil: return true

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

proc addParam*(procType: PType; param: PSym) =
param.position = procType.len-1
addSon(procType.n, newSymNode(param))
procType.n.add newSymNode(param)
rawAddSon(procType, param.typ)

template destructor*(t: PType): PSym = t.attachedOps[attachedDestructor]
Expand Down
Loading

0 comments on commit 7e747d1

Please sign in to comment.