From 7e747d11c66405f08cc7c69e5afc18348663275e Mon Sep 17 00:00:00 2001 From: Clyybber Date: Thu, 28 Nov 2019 17:13:04 +0100 Subject: 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 --- compiler/semtypinst.nim | 149 ++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 75 deletions(-) (limited to 'compiler/semtypinst.nim') diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim index f9908f90f..786a9e4f8 100644 --- a/compiler/semtypinst.nim +++ b/compiler/semtypinst.nim @@ -16,32 +16,32 @@ const tfInstClearedFlags = {tfHasMeta, tfUnresolved} proc checkPartialConstructedType(conf: ConfigRef; info: TLineInfo, t: PType) = - if t.kind in {tyVar, tyLent} and t.sons[0].kind in {tyVar, tyLent}: + if t.kind in {tyVar, tyLent} and t[0].kind in {tyVar, tyLent}: localError(conf, info, "type 'var var' is not allowed") proc checkConstructedType*(conf: ConfigRef; info: TLineInfo, typ: PType) = var t = typ.skipTypes({tyDistinct}) if t.kind in tyTypeClasses: discard - elif t.kind in {tyVar, tyLent} and t.sons[0].kind in {tyVar, tyLent}: + elif t.kind in {tyVar, tyLent} and t[0].kind in {tyVar, tyLent}: localError(conf, info, "type 'var var' is not allowed") elif computeSize(conf, t) == szIllegalRecursion or isTupleRecursive(t): localError(conf, info, "illegal recursion in type '" & typeToString(t) & "'") when false: - if t.kind == tyObject and t.sons[0] != nil: - if t.sons[0].kind != tyObject or tfFinal in t.sons[0].flags: + if t.kind == tyObject and t[0] != nil: + if t[0].kind != tyObject or tfFinal in t[0].flags: localError(info, errInheritanceOnlyWithNonFinalObjects) proc searchInstTypes*(key: PType): PType = - let genericTyp = key.sons[0] + let genericTyp = key[0] if not (genericTyp.kind == tyGenericBody and - key.sons[0] == genericTyp and genericTyp.sym != nil): return + key[0] == genericTyp and genericTyp.sym != nil): return when not defined(nimNoNilSeqs): if genericTyp.sym.typeInstCache == nil: return for inst in genericTyp.sym.typeInstCache: if inst.id == key.id: return inst - if inst.sons.len < key.sons.len: + if inst.len < key.len: # XXX: This happens for prematurely cached # types such as Channel[empty]. Why? # See the notes for PActor in handleGenericInvocation @@ -50,9 +50,9 @@ proc searchInstTypes*(key: PType): PType = continue block matchType: - for j in 1 .. high(key.sons): + for j in 1..high(key.sons): # XXX sameType is not really correct for nested generics? - if not compareTypes(inst.sons[j], key.sons[j], + if not compareTypes(inst[j], key[j], flags = {ExactGenericParams}): break matchType @@ -61,7 +61,7 @@ proc searchInstTypes*(key: PType): PType = proc cacheTypeInst*(inst: PType) = # XXX: add to module's generics # update the refcount - let gt = inst.sons[0] + let gt = inst[0] let t = if gt.kind == tyGenericBody: gt.lastSon else: gt if t.kind in {tyStatic, tyError, tyGenericParam} + tyTypeClasses: return @@ -129,7 +129,7 @@ proc prepareNode(cl: var TReplTypeVars, n: PNode): PNode = result.typ = t if result.kind == nkSym: result.sym = replaceTypeVarsS(cl, n.sym) let isCall = result.kind in nkCallKinds - for i in 0 ..< n.safeLen: + for i in 0.. 0: - newSons(result, length) + if n.len > 0: + newSons(result, n.len) if start > 0: - result.sons[0] = n.sons[0] - for i in start ..< length: - result.sons[i] = replaceTypeVarsN(cl, n.sons[i]) + result[0] = n[0] + for i in start.. 0: t.n.sons[i].sym.typ = skipped + t[i] = skipped + if i > 0: t.n[i].sym.typ = skipped # when the typeof operator is used on a static input # param, the results gets infected with static as well: - if t.sons[0] != nil and t.sons[0].kind == tyStatic: - t.sons[0] = t.sons[0].base + if t[0] != nil and t[0].kind == tyStatic: + t[0] = t[0].base proc propagateFieldFlags(t: PType, n: PNode) = # This is meant for objects and tuples @@ -563,8 +562,8 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType = result = makeTypeDesc(cl.c, result) elif tfUnresolved in t.flags or cl.skipTypedesc: result = result.base - elif t.sons[0].kind != tyNone: - result = makeTypeDesc(cl.c, replaceTypeVarsT(cl, t.sons[0])) + elif t[0].kind != tyNone: + result = makeTypeDesc(cl.c, replaceTypeVarsT(cl, t[0])) of tyUserTypeClass, tyStatic: result = t @@ -573,8 +572,8 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType = bailout() result = instCopyType(cl, t) idTablePut(cl.localCache, t, result) - for i in 1 ..< result.len: - result.sons[i] = replaceTypeVarsT(cl, result.sons[i]) + for i in 1.. 0 and t.sons[0] != nil: - let b = skipTypes(t.sons[0], skipPtrs) + if t != nil and t.len > 0 and t[0] != nil: + let b = skipTypes(t[0], skipPtrs) recomputeFieldPositions(b, b.n, currPosition) case obj.kind of nkRecList: - for i in 0 ..< len(obj): recomputeFieldPositions(nil, obj.sons[i], currPosition) + for i in 0..