unpublic the sons field of PType; the precursor to PType refactorings (#22446)

* unpublic the sons field of PType

* tiny fixes

* fixes an omittance

* fixes IC

* fixes
This commit is contained in:
ringabout
2023-08-11 22:18:24 +08:00
committed by GitHub
parent 72bc72bf9e
commit 469c9cfab4
21 changed files with 94 additions and 76 deletions

View File

@@ -958,7 +958,7 @@ type
kind*: TTypeKind # kind of type
callConv*: TCallingConvention # for procs
flags*: TTypeFlags # flags of the type
sons*: TTypeSeq # base types, etc.
sons: TTypeSeq # base types, etc.
n*: PNode # node for types:
# for range types a nkRange node
# for record types a nkRecord node
@@ -1537,15 +1537,31 @@ proc `$`*(s: PSym): string =
else:
result = "<nil>"
proc newType*(kind: TTypeKind, id: ItemId; owner: PSym): PType =
iterator items*(t: PType): PType =
for i in 0..<t.sons.len: yield t.sons[i]
iterator pairs*(n: PType): tuple[i: int, n: PType] =
for i in 0..<n.sons.len: yield (i, n.sons[i])
proc newType*(kind: TTypeKind, id: ItemId; owner: PSym, sons: seq[PType] = @[]): PType =
result = PType(kind: kind, owner: owner, size: defaultSize,
align: defaultAlignment, itemId: id,
uniqueId: id)
uniqueId: id, sons: sons)
when false:
if result.itemId.module == 55 and result.itemId.item == 2:
echo "KNID ", kind
writeStackTrace()
template newType*(kind: TTypeKind, id: ItemId; owner: PSym, parent: PType): PType =
newType(kind, id, owner, parent.sons)
proc newType*(prev: PType, sons: seq[PType]): PType =
result = prev
result.sons = sons
proc addSon*(father, son: PType) =
# todo fixme: in IC, `son` might be nil
father.sons.add(son)
proc mergeLoc(a: var TLoc, b: TLoc) =
if a.k == low(typeof(a.k)): a.k = b.k

View File

@@ -3190,9 +3190,9 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Rope) =
result.add "}"
of tyArray:
result.add "{"
for i in 0..<toInt(lengthOrd(p.config, t.sons[0])):
for i in 0..<toInt(lengthOrd(p.config, t[0])):
if i > 0: result.add ", "
getDefaultValue(p, t.sons[1], info, result)
getDefaultValue(p, t[1], info, result)
result.add "}"
#result = rope"{}"
of tyOpenArray, tyVarargs:

View File

@@ -1178,9 +1178,9 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Rope; asPtr: bool =
var memberOp = "#." #only virtual
var typ: PType
if isCtor:
typ = prc.typ.sons[0]
typ = prc.typ[0]
else:
typ = prc.typ.sons[1]
typ = prc.typ[1]
if typ.kind == tyPtr:
typ = typ[0]
memberOp = "#->"

View File

@@ -167,9 +167,9 @@ proc matchType(c: PContext; f, a: PType; m: var MatchCon): bool =
# modifiers in the concept must be there in the actual implementation
# too but not vice versa.
if a.kind == f.kind:
result = matchType(c, f.sons[0], a.sons[0], m)
result = matchType(c, f[0], a[0], m)
elif m.magic == mArrPut:
result = matchType(c, f.sons[0], a, m)
result = matchType(c, f[0], a, m)
else:
result = false
of tyEnum, tyObject, tyDistinct:
@@ -264,7 +264,7 @@ proc matchSym(c: PContext; candidate: PSym, n: PNode; m: var MatchCon): bool =
m.inferred.setLen oldLen
return false
if not matchReturnType(c, n[0].sym.typ.sons[0], candidate.typ.sons[0], m):
if not matchReturnType(c, n[0].sym.typ[0], candidate.typ[0], m):
m.inferred.setLen oldLen
return false

View File

@@ -1196,9 +1196,9 @@ proc genJsonItem(d: PDoc, n, nameNode: PNode, k: TSymKind, nonExports = false):
result.json["signature"]["genericParams"] = newJArray()
for genericParam in n[genericParamsPos]:
var param = %{"name": %($genericParam)}
if genericParam.sym.typ.sons.len > 0:
if genericParam.sym.typ.len > 0:
param["types"] = newJArray()
for kind in genericParam.sym.typ.sons:
for kind in genericParam.sym.typ:
param["types"].add %($kind)
result.json["signature"]["genericParams"].add param
if optGenIndex in d.conf.globalOptions:

View File

@@ -359,7 +359,7 @@ proc storeType(t: PType; c: var PackedEncoder; m: var PackedModule): PackedItemI
paddingAtEnd: t.paddingAtEnd)
storeNode(p, t, n)
p.typeInst = t.typeInst.storeType(c, m)
for kid in items t.sons:
for kid in items t:
p.types.add kid.storeType(c, m)
c.addMissing t.sym
p.sym = t.sym.safeItemId(c, m)
@@ -917,7 +917,7 @@ proc typeBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph;
result.attachedOps[op] = loadSym(c, g, si, item)
result.typeInst = loadType(c, g, si, t.typeInst)
for son in items t.types:
result.sons.add loadType(c, g, si, son)
result.addSon loadType(c, g, si, son)
loadAstBody(t, n)
when false:
for gen, id in items t.methods:

View File

@@ -302,7 +302,7 @@ proc newHookCall(c: var TLiftCtx; op: PSym; x, y: PNode): PNode =
result.add newSymNode(op)
if sfNeverRaises notin op.flags:
c.canRaise = true
if op.typ.sons[1].kind == tyVar:
if op.typ[1].kind == tyVar:
result.add genAddr(c, x)
else:
result.add x

View File

@@ -100,7 +100,7 @@ proc skipIntLit*(t: PType; id: IdGenerator): PType {.inline.} =
proc addSonSkipIntLit*(father, son: PType; id: IdGenerator) =
let s = son.skipIntLit(id)
father.sons.add(s)
father.add(s)
propagateToOwner(father, s)
proc getCompilerProc*(g: ModuleGraph; name: string): PSym =

View File

@@ -140,9 +140,9 @@ proc pragmaEnsures(c: PContext, n: PNode) =
else:
openScope(c)
let o = getCurrOwner(c)
if o.kind in routineKinds and o.typ != nil and o.typ.sons[0] != nil:
if o.kind in routineKinds and o.typ != nil and o.typ[0] != nil:
var s = newSym(skResult, getIdent(c.cache, "result"), c.idgen, o, n.info)
s.typ = o.typ.sons[0]
s.typ = o.typ[0]
incl(s.flags, sfUsed)
addDecl(c, s)
n[1] = c.semExpr(c, n[1])

View File

@@ -598,7 +598,7 @@ proc inheritBindings(c: PContext, x: var TCandidate, expectedType: PType) =
# nested, add all the types to stack
let
startIdx = if u.kind in ConcreteTypes: 0 else: 1
endIdx = min(u.sons.len() - startIdx, t.sons.len())
endIdx = min(u.len() - startIdx, t.len())
for i in startIdx ..< endIdx:
# early exit with current impl
@@ -717,8 +717,7 @@ proc explicitGenericSym(c: PContext, n: PNode, s: PSym): PNode =
if formal.kind == tyStatic and arg.kind != tyStatic:
let evaluated = c.semTryConstExpr(c, n[i])
if evaluated != nil:
arg = newTypeS(tyStatic, c)
arg.sons = @[evaluated.typ]
arg = newTypeS(tyStatic, c, sons = @[evaluated.typ])
arg.n = evaluated
let tm = typeRel(m, formal, arg)
if tm in {isNone, isConvertible}: return nil

View File

@@ -395,8 +395,8 @@ proc addToLib*(lib: PLib, sym: PSym) =
# LocalError(sym.info, errInvalidPragma)
sym.annex = lib
proc newTypeS*(kind: TTypeKind, c: PContext): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c))
proc newTypeS*(kind: TTypeKind, c: PContext, sons: seq[PType] = @[]): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), sons = sons)
proc makePtrType*(owner: PSym, baseType: PType; idgen: IdGenerator): PType =
result = newType(tyPtr, nextTypeId(idgen), owner)
@@ -446,13 +446,15 @@ proc makeTypeFromExpr*(c: PContext, n: PNode): PType =
proc newTypeWithSons*(owner: PSym, kind: TTypeKind, sons: seq[PType];
idgen: IdGenerator): PType =
result = newType(kind, nextTypeId(idgen), owner)
result.sons = sons
result = newType(kind, nextTypeId(idgen), owner, sons = sons)
proc newTypeWithSons*(c: PContext, kind: TTypeKind,
sons: seq[PType]): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c))
result.sons = sons
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), sons = sons)
proc newTypeWithSons*(c: PContext, kind: TTypeKind,
parent: PType): PType =
result = newType(kind, nextTypeId(c.idgen), getCurrOwner(c), parent = parent)
proc makeStaticExpr*(c: PContext, n: PNode): PNode =
result = newNodeI(nkStaticExpr, n.info)
@@ -461,21 +463,21 @@ proc makeStaticExpr*(c: PContext, n: PNode): PNode =
else: newTypeWithSons(c, tyStatic, @[n.typ])
proc makeAndType*(c: PContext, t1, t2: PType): PType =
result = newTypeS(tyAnd, c)
result.sons = @[t1, t2]
result = newTypeS(tyAnd, c, sons = @[t1, t2])
propagateToOwner(result, t1)
propagateToOwner(result, t2)
result.flags.incl((t1.flags + t2.flags) * {tfHasStatic})
result.flags.incl tfHasMeta
proc makeOrType*(c: PContext, t1, t2: PType): PType =
result = newTypeS(tyOr, c)
if t1.kind != tyOr and t2.kind != tyOr:
result.sons = @[t1, t2]
result = newTypeS(tyOr, c, sons = @[t1, t2])
else:
result = newTypeS(tyOr, c)
template addOr(t1) =
if t1.kind == tyOr:
for x in t1.sons: result.rawAddSon x
for x in t1: result.rawAddSon x
else:
result.rawAddSon t1
addOr(t1)
@@ -486,8 +488,7 @@ proc makeOrType*(c: PContext, t1, t2: PType): PType =
result.flags.incl tfHasMeta
proc makeNotType*(c: PContext, t1: PType): PType =
result = newTypeS(tyNot, c)
result.sons = @[t1]
result = newTypeS(tyNot, c, sons = @[t1])
propagateToOwner(result, t1)
result.flags.incl(t1.flags * {tfHasStatic})
result.flags.incl tfHasMeta
@@ -498,8 +499,7 @@ proc nMinusOne(c: PContext; n: PNode): PNode =
# Remember to fix the procs below this one when you make changes!
proc makeRangeWithStaticExpr*(c: PContext, n: PNode): PType =
let intType = getSysType(c.graph, n.info, tyInt)
result = newTypeS(tyRange, c)
result.sons = @[intType]
result = newTypeS(tyRange, c, sons = @[intType])
if n.typ != nil and n.typ.n == nil:
result.flags.incl tfUnresolved
result.n = newTreeI(nkRange, n.info, newIntTypeNode(0, intType),

View File

@@ -1422,7 +1422,7 @@ proc tryReadingTypeField(c: PContext, n: PNode, i: PIdent, ty: PType): PNode =
while ty != nil:
f = getSymFromList(ty.n, i)
if f != nil: break
ty = ty.sons[0] # enum inheritance
ty = ty[0] # enum inheritance
if f != nil:
result = newSymNode(f)
result.info = n.info

View File

@@ -1327,7 +1327,7 @@ proc track(tracked: PEffects, n: PNode) =
proc subtypeRelation(g: ModuleGraph; spec, real: PNode): bool =
if spec.typ.kind == tyOr:
for t in spec.typ.sons:
for t in spec.typ:
if safeInheritanceDiff(g.excType(real), t) <= 0:
return true
else:

View File

@@ -45,7 +45,7 @@ proc hasEmpty(typ: PType): bool =
result = typ.lastSon.kind == tyEmpty
elif typ.kind == tyTuple:
result = false
for s in typ.sons:
for s in typ:
result = result or hasEmpty(s)
else:
result = false
@@ -1344,7 +1344,7 @@ proc checkCovariantParamsUsages(c: PContext; genericType: PType) =
of tyArray:
return traverseSubTypes(c, t[1])
of tyProc:
for subType in t.sons:
for subType in t:
if subType != nil:
subresult traverseSubTypes(c, subType)
if result:
@@ -1377,7 +1377,7 @@ proc checkCovariantParamsUsages(c: PContext; genericType: PType) =
of tyUserTypeClass, tyUserTypeClassInst:
error("non-invariant type parameters are not supported in concepts")
of tyTuple:
for fieldType in t.sons:
for fieldType in t:
subresult traverseSubTypes(c, fieldType)
of tyPtr, tyRef, tyVar, tyLent:
if t.base.kind == tyGenericParam: return true
@@ -2283,18 +2283,18 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
let isCtor = sfConstructor in s.flags
let pragmaName = if isVirtual: "virtual" elif isCtor: "constructor" else: "member"
if c.config.backend == backendCpp:
if s.typ.sons.len < 2 and not isCtor:
if s.typ.len < 2 and not isCtor:
localError(c.config, n.info, pragmaName & " must have at least one parameter")
for son in s.typ.sons:
for son in s.typ:
if son!=nil and son.isMetaType:
localError(c.config, n.info, pragmaName & " unsupported for generic routine")
var typ: PType
if isCtor:
typ = s.typ.sons[0]
typ = s.typ[0]
if typ == nil or typ.kind != tyObject:
localError(c.config, n.info, "constructor must return an object")
else:
typ = s.typ.sons[1]
typ = s.typ[1]
if typ.kind == tyPtr and not isCtor:
typ = typ[0]
if typ.kind != tyObject:

View File

@@ -38,13 +38,20 @@ const
errNoGenericParamsAllowedForX = "no generic parameters allowed for $1"
errInOutFlagNotExtern = "the '$1' modifier can be used only with imported types"
proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext, sons: seq[PType]): PType =
if prev == nil or prev.kind == tyGenericBody:
result = newTypeS(kind, c, sons = sons)
else:
result = newType(prev, sons)
if result.kind == tyForward: result.kind = kind
#if kind == tyError: result.flags.incl tfCheckedForDestructor
proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
if prev == nil or prev.kind == tyGenericBody:
result = newTypeS(kind, c)
else:
result = prev
if result.kind == tyForward: result.kind = kind
#if kind == tyError: result.flags.incl tfCheckedForDestructor
proc newConstraint(c: PContext, k: TTypeKind): PType =
result = newTypeS(tyBuiltInTypeClass, c)
@@ -245,7 +252,7 @@ proc isRecursiveType*(t: PType): bool =
proc addSonSkipIntLitChecked(c: PContext; father, son: PType; it: PNode, id: IdGenerator) =
let s = son.skipIntLit(id)
father.sons.add(s)
father.add(s)
if isRecursiveType(s):
localError(c.config, it.info, "illegal recursion in type '" & typeToString(s) & "'")
else:
@@ -1012,7 +1019,7 @@ proc findEnforcedStaticType(t: PType): PType =
if t == nil: return nil
if t.kind == tyStatic: return t
if t.kind == tyAnd:
for s in t.sons:
for s in t:
let t = findEnforcedStaticType(s)
if t != nil: return t
@@ -1658,11 +1665,10 @@ proc semTypeClass(c: PContext, n: PNode, prev: PType): PType =
pragmas = n[1]
inherited = n[2]
result = newOrPrevType(tyUserTypeClass, prev, c)
result.flags.incl tfCheckedForDestructor
var owner = getCurrOwner(c)
var candidateTypeSlot = newTypeWithSons(owner, tyAlias, @[c.errorType], c.idgen)
result.sons = @[candidateTypeSlot]
result = newOrPrevType(tyUserTypeClass, prev, c, sons = @[candidateTypeSlot])
result.flags.incl tfCheckedForDestructor
result.n = n
if inherited.kind != nkEmpty:

View File

@@ -52,7 +52,7 @@ proc searchInstTypes*(g: ModuleGraph; key: PType): PType =
continue
block matchType:
for j in 1..high(key.sons):
for j in 1..<len(key):
# XXX sameType is not really correct for nested generics?
if not compareTypes(inst[j], key[j],
flags = {ExactGenericParams, PickyCAliases}):
@@ -384,10 +384,9 @@ proc handleGenericInvocation(cl: var TReplTypeVars, t: PType): PType =
else:
header = instCopyType(cl, t)
result = newType(tyGenericInst, nextTypeId(cl.c.idgen), t[0].owner)
result = newType(tyGenericInst, nextTypeId(cl.c.idgen), t[0].owner, sons = @[header[0]])
result.flags = header.flags
# be careful not to propagate unnecessary flags here (don't use rawAddSon)
result.sons = @[header[0]]
# ugh need another pass for deeply recursive generic types (e.g. PActor)
# we need to add the candidate here, before it's fully instantiated for
# recursive instantions:
@@ -486,7 +485,7 @@ proc eraseVoidParams*(t: PType) =
t[pos] = t[j]
t.n[pos] = t.n[j]
inc pos
setLen t.sons, pos
newSons t, pos
setLen t.n.sons, pos
break
@@ -590,8 +589,7 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
# return tyStatic values to let anyone make
# use of this knowledge. The patching here
# won't be necessary then.
result = newTypeS(tyStatic, cl.c)
result.sons = @[n.typ]
result = newTypeS(tyStatic, cl.c, sons = @[n.typ])
result.n = n
else:
result = n.typ

View File

@@ -187,7 +187,7 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]; conf: Confi
hashType c, t[0], flags, conf
of tyRef, tyPtr, tyGenericBody, tyVar:
c &= char(t.kind)
if t.sons.len > 0:
if t.len > 0:
c.hashType t.lastSon, flags, conf
if tfVarIsPtr in t.flags: c &= ".varisptr"
of tyFromExpr:

View File

@@ -228,7 +228,7 @@ proc sumGeneric(t: PType): int =
inc result
of tyOr:
var maxBranch = 0
for branch in t.sons:
for branch in t:
let branchSum = sumGeneric(branch)
if branchSum > maxBranch: maxBranch = branchSum
inc result, maxBranch
@@ -904,7 +904,7 @@ proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool =
else: discard
elif lhs.kind == nkSym and lhs.typ.kind == tyStatic and lhs.typ.n == nil:
var inferred = newTypeWithSons(c.c, tyStatic, lhs.typ.sons)
var inferred = newTypeWithSons(c.c, tyStatic, lhs.typ)
inferred.n = newIntNode(nkIntLit, rhs)
put(c, lhs.typ, inferred)
if c.c.matchedConcept != nil:
@@ -1109,7 +1109,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
# both int and string must match against number
# but ensure that '[T: A|A]' matches as good as '[T: A]' (bug #2219):
result = isGeneric
for branch in a.sons:
for branch in a:
let x = typeRel(c, f, branch, flags + {trDontBind})
if x == isNone: return isNone
if x < result: result = x
@@ -1120,7 +1120,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
c.typedescMatched = true
# seq[Sortable and Iterable] vs seq[Sortable]
# only one match is enough
for branch in a.sons:
for branch in a:
let x = typeRel(c, f, branch, flags + {trDontBind})
if x != isNone:
return if x >= isGeneric: isGeneric else: x
@@ -1622,7 +1622,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
of tyAnd:
considerPreviousT:
result = isEqual
for branch in f.sons:
for branch in f:
let x = typeRel(c, branch, aOrig, flags)
if x < isSubtype: return isNone
# 'and' implies minimum matching result:
@@ -1635,7 +1635,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
result = isNone
let oldInheritancePenalty = c.inheritancePenalty
var maxInheritance = 0
for branch in f.sons:
for branch in f:
c.inheritancePenalty = 0
let x = typeRel(c, branch, aOrig, flags)
maxInheritance = max(maxInheritance, c.inheritancePenalty)
@@ -1650,7 +1650,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
of tyNot:
considerPreviousT:
for branch in f.sons:
for branch in f:
if typeRel(c, branch, aOrig, flags) != isNone:
return isNone
@@ -2121,8 +2121,7 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType,
if evaluated != nil:
# Don't build the type in-place because `evaluated` and `arg` may point
# to the same object and we'd end up creating recursive types (#9255)
let typ = newTypeS(tyStatic, c)
typ.sons = @[evaluated.typ]
let typ = newTypeS(tyStatic, c, sons = @[evaluated.typ])
typ.n = evaluated
arg = copyTree(arg) # fix #12864
arg.typ = typ
@@ -2714,7 +2713,7 @@ proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
# forget all inferred types if the overload matching failed
if m.state == csNoMatch:
for t in m.inferredTypes:
if t.len > 1: t.sons.setLen 1
if t.len > 1: t.newSons 1
proc argtypeMatches*(c: PContext, f, a: PType, fromHlo = false): bool =
var m = newCandidate(c, f)

View File

@@ -566,7 +566,7 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
if t.kind == tyGenericParam and t.len > 0:
result.add ": "
var first = true
for son in t.sons:
for son in t:
if not first: result.add " or "
result.add son.typeToString
first = false
@@ -637,14 +637,14 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string =
result.add(typeToString(t[i]))
result.add "]"
of tyAnd:
for i, son in t.sons:
for i, son in t:
result.add(typeToString(son))
if i < t.sons.high:
if i < t.len - 1:
result.add(" and ")
of tyOr:
for i, son in t.sons:
for i, son in t:
result.add(typeToString(son))
if i < t.sons.high:
if i < t.len - 1:
result.add(" or ")
of tyNot:
result = "not " & typeToString(t[0])

View File

@@ -407,8 +407,8 @@ proc allRoots(n: PNode; result: var seq[(PSym, int)]; level: int) =
if typ != nil and i < typ.len:
assert(typ.n[i].kind == nkSym)
let paramType = typ.n[i].typ
if not paramType.isCompileTimeOnly and not typ.sons[0].isEmptyType and
canAlias(paramType, typ.sons[0]):
if not paramType.isCompileTimeOnly and not typ[0].isEmptyType and
canAlias(paramType, typ[0]):
allRoots(it, result, RootEscapes)
else:
allRoots(it, result, RootEscapes)

View File

@@ -199,7 +199,7 @@ proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
# only named tuples have a node, unnamed tuples don't
if t.n.isNil:
result = newNodeX(nkTupleConstr)
for subType in t.sons:
for subType in t:
result.add mapTypeToAst(subType, info)
else:
result = newNodeX(nkTupleTy)