semtypes.nim: more refactorings for liftParamType

This commit is contained in:
Andreas Rumpf 2019-08-25 13:01:27 +02:00
commit 97d77bf37e

View file

@ -899,12 +899,9 @@ template shouldHaveMeta(t) =
internalAssert c.config, tfHasMeta in t.flags internalAssert c.config, tfHasMeta in t.flags
# result.lastSon.flags.incl tfHasMeta # result.lastSon.flags.incl tfHasMeta
proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode, proc addImplicitGeneric(c: PContext; typeClass: PType, typId: PIdent;
paramType: PType, paramName: string, info: TLineInfo; genericParams: PNode;
info: TLineInfo, anon = false): PType = paramName: string): PType =
if paramType == nil: return # (e.g. proc return type)
proc addImplicitGenericImpl(c: PContext; typeClass: PType, typId: PIdent): PType =
if genericParams == nil: if genericParams == nil:
# This happens with anonymous proc types appearing in signatures # This happens with anonymous proc types appearing in signatures
# XXX: we need to lift these earlier # XXX: we need to lift these earlier
@ -928,6 +925,11 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
result = typeClass result = typeClass
addDecl(c, s) addDecl(c, s)
proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
paramType: PType, paramName: string,
info: TLineInfo, anon = false): PType =
if paramType == nil: return # (e.g. proc return type)
template recurse(typ: PType, anonFlag = false): untyped = template recurse(typ: PType, anonFlag = false): untyped =
liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag) liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)
@ -936,7 +938,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
case paramType.kind: case paramType.kind:
of tyAnything: of tyAnything:
result = addImplicitGenericImpl(c, newTypeS(tyGenericParam, c), nil) result = addImplicitGeneric(c, newTypeS(tyGenericParam, c), nil, info, genericParams, paramName)
of tyStatic: of tyStatic:
if paramType.base.kind != tyNone and paramType.n != nil: if paramType.base.kind != tyNone and paramType.n != nil:
@ -948,7 +950,8 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
let base = (if lifted != nil: lifted else: paramType.base) let base = (if lifted != nil: lifted else: paramType.base)
if base.isMetaType and procKind == skMacro: if base.isMetaType and procKind == skMacro:
localError(c.config, info, errMacroBodyDependsOnGenericTypes % paramName) localError(c.config, info, errMacroBodyDependsOnGenericTypes % paramName)
result = addImplicitGenericImpl(c, c.newTypeWithSons(tyStatic, @[base]), paramTypId) result = addImplicitGeneric(c, c.newTypeWithSons(tyStatic, @[base]),
paramTypId, info, genericParams, paramName)
if result != nil: result.flags.incl({tfHasStatic, tfUnresolved}) if result != nil: result.flags.incl({tfHasStatic, tfUnresolved})
of tyTypeDesc: of tyTypeDesc:
@ -961,7 +964,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
paramTypId = nil paramTypId = nil
let t = c.newTypeWithSons(tyTypeDesc, @[paramType.base]) let t = c.newTypeWithSons(tyTypeDesc, @[paramType.base])
incl t.flags, tfCheckedForDestructor incl t.flags, tfCheckedForDestructor
result = addImplicitGenericImpl(c, t, paramTypId) result = addImplicitGeneric(c, t, paramTypId, info, genericParams, paramName)
of tyDistinct: of tyDistinct:
if paramType.sonsLen == 1: if paramType.sonsLen == 1:
@ -982,7 +985,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
if paramType.kind == tySequence and paramType.lastSon.kind == tyNone: if paramType.kind == tySequence and paramType.lastSon.kind == tyNone:
let typ = c.newTypeWithSons(tyBuiltInTypeClass, let typ = c.newTypeWithSons(tyBuiltInTypeClass,
@[newTypeS(paramType.kind, c)]) @[newTypeS(paramType.kind, c)])
result = addImplicitGenericImpl(c, typ, paramTypId) result = addImplicitGeneric(c, typ, paramTypId, info, genericParams, paramName)
else: else:
for i in 0 ..< paramType.len: for i in 0 ..< paramType.len:
if paramType.sons[i] == paramType: if paramType.sons[i] == paramType:
@ -1007,20 +1010,20 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
if paramType.lastSon.kind == tyUserTypeClass: if paramType.lastSon.kind == tyUserTypeClass:
result.kind = tyUserTypeClassInst result.kind = tyUserTypeClassInst
result.rawAddSon paramType.lastSon result.rawAddSon paramType.lastSon
return addImplicitGenericImpl(c, result, paramTypId) return addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)
let x = instGenericContainer(c, paramType.sym.info, result, let x = instGenericContainer(c, paramType.sym.info, result,
allowMetaTypes = true) allowMetaTypes = true)
result = newTypeWithSons(c, tyCompositeTypeClass, @[paramType, x]) result = newTypeWithSons(c, tyCompositeTypeClass, @[paramType, x])
#result = newTypeS(tyCompositeTypeClass, c) #result = newTypeS(tyCompositeTypeClass, c)
#for i in 0..<x.len: result.rawAddSon(x.sons[i]) #for i in 0..<x.len: result.rawAddSon(x.sons[i])
result = addImplicitGenericImpl(c, result, paramTypId) result = addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)
of tyGenericInst: of tyGenericInst:
if paramType.lastSon.kind == tyUserTypeClass: if paramType.lastSon.kind == tyUserTypeClass:
var cp = copyType(paramType, getCurrOwner(c), false) var cp = copyType(paramType, getCurrOwner(c), false)
cp.kind = tyUserTypeClassInst cp.kind = tyUserTypeClassInst
return addImplicitGenericImpl(c, cp, paramTypId) return addImplicitGeneric(c, cp, paramTypId, info, genericParams, paramName)
for i in 1 .. paramType.len-2: for i in 1 .. paramType.len-2:
var lifted = recurse(paramType.sons[i]) var lifted = recurse(paramType.sons[i])
@ -1053,8 +1056,9 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass, of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
tyAnd, tyOr, tyNot: tyAnd, tyOr, tyNot:
result = addImplicitGenericImpl(c, result = addImplicitGeneric(c,
copyType(paramType, getCurrOwner(c), false), paramTypId) copyType(paramType, getCurrOwner(c), false), paramTypId,
info, genericParams, paramName)
of tyGenericParam: of tyGenericParam:
markUsed(c, paramType.sym.info, paramType.sym) markUsed(c, paramType.sym.info, paramType.sym)
@ -1065,8 +1069,6 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
else: discard else: discard
# result = recurse(paramType)
proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType = proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType =
if n.kind == nkCurlyExpr: if n.kind == nkCurlyExpr:
result = semTypeNode(c, n.sons[0], nil) result = semTypeNode(c, n.sons[0], nil)