[other] make typeAllowed easier to debug by using structured programming

This commit is contained in:
Araq 2019-06-11 15:00:47 +02:00
commit 549d8cc0c6

View file

@ -1204,30 +1204,35 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
# if we have already checked the type, return true, because we stop the # if we have already checked the type, return true, because we stop the
# evaluation if something is wrong: # evaluation if something is wrong:
result = nil result = nil
if typ == nil: return if typ == nil: return nil
if containsOrIncl(marker, typ.id): return if containsOrIncl(marker, typ.id): return nil
var t = skipTypes(typ, abstractInst-{tyTypeDesc}) var t = skipTypes(typ, abstractInst-{tyTypeDesc})
case t.kind case t.kind
of tyVar, tyLent: of tyVar, tyLent:
if kind in {skProc, skFunc, skConst}: return t if kind in {skProc, skFunc, skConst}:
elif t.kind == tyLent and kind != skResult: return t result = t
var t2 = skipTypes(t.sons[0], abstractInst-{tyTypeDesc}) elif t.kind == tyLent and kind != skResult:
case t2.kind result = t
of tyVar, tyLent:
if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
of tyOpenArray, tyUncheckedArray:
if kind != skParam: result = t
else: result = typeAllowedAux(marker, t2.sons[0], skParam, flags)
else: else:
if kind notin {skParam, skResult}: result = t var t2 = skipTypes(t.sons[0], abstractInst-{tyTypeDesc})
else: result = typeAllowedAux(marker, t2, kind, flags) case t2.kind
of tyVar, tyLent:
if taHeap notin flags: result = t2 # ``var var`` is illegal on the heap
of tyOpenArray, tyUncheckedArray:
if kind != skParam: result = t
else: result = typeAllowedAux(marker, t2.sons[0], skParam, flags)
else:
if kind notin {skParam, skResult}: result = t
else: result = typeAllowedAux(marker, t2, kind, flags)
of tyProc: of tyProc:
if kind == skConst and t.callConv == ccClosure: return t if kind == skConst and t.callConv == ccClosure:
for i in 1 ..< sonsLen(t): result = t
result = typeAllowedAux(marker, t.sons[i], skParam, flags) else:
if result != nil: break for i in 1 ..< sonsLen(t):
if result.isNil and t.sons[0] != nil: result = typeAllowedAux(marker, t.sons[i], skParam, flags)
result = typeAllowedAux(marker, t.sons[0], skResult, flags) if result != nil: break
if result.isNil and t.sons[0] != nil:
result = typeAllowedAux(marker, t.sons[0], skResult, flags)
of tyTypeDesc: of tyTypeDesc:
# XXX: This is still a horrible idea... # XXX: This is still a horrible idea...
result = nil result = nil
@ -1287,13 +1292,15 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind,
if result != nil: break if result != nil: break
of tyObject, tyTuple: of tyObject, tyTuple:
if kind in {skProc, skFunc, skConst} and if kind in {skProc, skFunc, skConst} and
t.kind == tyObject and t.sons[0] != nil: return t t.kind == tyObject and t.sons[0] != nil:
let flags = flags+{taField} result = t
for i in 0 ..< sonsLen(t): else:
result = typeAllowedAux(marker, t.sons[i], kind, flags) let flags = flags+{taField}
if result != nil: break for i in 0 ..< sonsLen(t):
if result.isNil and t.n != nil: result = typeAllowedAux(marker, t.sons[i], kind, flags)
result = typeAllowedNode(marker, t.n, kind, flags) if result != nil: break
if result.isNil and t.n != nil:
result = typeAllowedNode(marker, t.n, kind, flags)
of tyEmpty: of tyEmpty:
if kind in {skVar, skLet}: result = t if kind in {skVar, skLet}: result = t
of tyProxy: of tyProxy: