semtypes.nim: little refactoring for liftParamType
This commit is contained in:
parent
d8d2a9e985
commit
0c41fd0f50
1 changed files with 20 additions and 26 deletions
|
|
@ -928,21 +928,12 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
result = typeClass
|
result = typeClass
|
||||||
addDecl(c, s)
|
addDecl(c, s)
|
||||||
|
|
||||||
# XXX: There are codegen errors if this is turned into a nested proc
|
template recurse(typ: PType, anonFlag = false): untyped =
|
||||||
template liftingWalk(typ: PType, anonFlag = false): untyped =
|
|
||||||
liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)
|
liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)
|
||||||
#proc liftingWalk(paramType: PType, anon = false): PType =
|
|
||||||
|
|
||||||
var paramTypId = if not anon and paramType.sym != nil: paramType.sym.name
|
var paramTypId = if not anon and paramType.sym != nil: paramType.sym.name
|
||||||
else: nil
|
else: nil
|
||||||
|
|
||||||
template maybeLift(typ: PType): untyped =
|
|
||||||
let lifted = liftingWalk(typ)
|
|
||||||
(if lifted != nil: lifted else: typ)
|
|
||||||
|
|
||||||
template addImplicitGeneric(e): untyped =
|
|
||||||
addImplicitGenericImpl(c, e, paramTypId)
|
|
||||||
|
|
||||||
case paramType.kind:
|
case paramType.kind:
|
||||||
of tyAnything:
|
of tyAnything:
|
||||||
result = addImplicitGenericImpl(c, newTypeS(tyGenericParam, c), nil)
|
result = addImplicitGenericImpl(c, newTypeS(tyGenericParam, c), nil)
|
||||||
|
|
@ -952,10 +943,12 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
# this is a concrete static value
|
# this is a concrete static value
|
||||||
return
|
return
|
||||||
if tfUnresolved in paramType.flags: return # already lifted
|
if tfUnresolved in paramType.flags: return # already lifted
|
||||||
let base = paramType.base.maybeLift
|
|
||||||
|
let lifted = recurse(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 = addImplicitGeneric(c.newTypeWithSons(tyStatic, @[base]))
|
result = addImplicitGenericImpl(c, c.newTypeWithSons(tyStatic, @[base]), paramTypId)
|
||||||
if result != nil: result.flags.incl({tfHasStatic, tfUnresolved})
|
if result != nil: result.flags.incl({tfHasStatic, tfUnresolved})
|
||||||
|
|
||||||
of tyTypeDesc:
|
of tyTypeDesc:
|
||||||
|
|
@ -968,15 +961,15 @@ 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 = addImplicitGeneric(t)
|
result = addImplicitGenericImpl(c, t, paramTypId)
|
||||||
|
|
||||||
of tyDistinct:
|
of tyDistinct:
|
||||||
if paramType.sonsLen == 1:
|
if paramType.sonsLen == 1:
|
||||||
# disable the bindOnce behavior for the type class
|
# disable the bindOnce behavior for the type class
|
||||||
result = liftingWalk(paramType.base, true)
|
result = recurse(paramType.base, true)
|
||||||
|
|
||||||
of tyAlias, tyOwned:
|
of tyAlias, tyOwned:
|
||||||
result = liftingWalk(paramType.base)
|
result = recurse(paramType.base)
|
||||||
|
|
||||||
of tySequence, tySet, tyArray, tyOpenArray,
|
of tySequence, tySet, tyArray, tyOpenArray,
|
||||||
tyVar, tyLent, tyPtr, tyRef, tyProc:
|
tyVar, tyLent, tyPtr, tyRef, tyProc:
|
||||||
|
|
@ -989,12 +982,12 @@ 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 = addImplicitGeneric(typ)
|
result = addImplicitGenericImpl(c, typ, paramTypId)
|
||||||
else:
|
else:
|
||||||
for i in 0 ..< paramType.len:
|
for i in 0 ..< paramType.len:
|
||||||
if paramType.sons[i] == paramType:
|
if paramType.sons[i] == paramType:
|
||||||
globalError(c.config, info, errIllegalRecursionInTypeX % typeToString(paramType))
|
globalError(c.config, info, errIllegalRecursionInTypeX % typeToString(paramType))
|
||||||
var lifted = liftingWalk(paramType.sons[i])
|
var lifted = recurse(paramType.sons[i])
|
||||||
if lifted != nil:
|
if lifted != nil:
|
||||||
paramType.sons[i] = lifted
|
paramType.sons[i] = lifted
|
||||||
result = paramType
|
result = paramType
|
||||||
|
|
@ -1014,29 +1007,29 @@ 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 addImplicitGeneric(result)
|
return addImplicitGenericImpl(c, result, paramTypId)
|
||||||
|
|
||||||
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 = addImplicitGeneric(result)
|
result = addImplicitGenericImpl(c, result, paramTypId)
|
||||||
|
|
||||||
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 addImplicitGeneric(cp)
|
return addImplicitGenericImpl(c, cp, paramTypId)
|
||||||
|
|
||||||
for i in 1 .. paramType.len-2:
|
for i in 1 .. paramType.len-2:
|
||||||
var lifted = liftingWalk(paramType.sons[i])
|
var lifted = recurse(paramType.sons[i])
|
||||||
if lifted != nil:
|
if lifted != nil:
|
||||||
paramType.sons[i] = lifted
|
paramType.sons[i] = lifted
|
||||||
result = paramType
|
result = paramType
|
||||||
result.lastSon.shouldHaveMeta
|
result.lastSon.shouldHaveMeta
|
||||||
|
|
||||||
let liftBody = liftingWalk(paramType.lastSon, true)
|
let liftBody = recurse(paramType.lastSon, true)
|
||||||
if liftBody != nil:
|
if liftBody != nil:
|
||||||
result = liftBody
|
result = liftBody
|
||||||
result.flags.incl tfHasMeta
|
result.flags.incl tfHasMeta
|
||||||
|
|
@ -1044,7 +1037,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
|
|
||||||
of tyGenericInvocation:
|
of tyGenericInvocation:
|
||||||
for i in 1 ..< paramType.len:
|
for i in 1 ..< paramType.len:
|
||||||
let lifted = liftingWalk(paramType.sons[i])
|
let lifted = recurse(paramType.sons[i])
|
||||||
if lifted != nil: paramType.sons[i] = lifted
|
if lifted != nil: paramType.sons[i] = lifted
|
||||||
|
|
||||||
let body = paramType.base
|
let body = paramType.base
|
||||||
|
|
@ -1056,11 +1049,12 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
if body.lastSon.kind == tyUserTypeClass:
|
if body.lastSon.kind == tyUserTypeClass:
|
||||||
let expanded = instGenericContainer(c, info, paramType,
|
let expanded = instGenericContainer(c, info, paramType,
|
||||||
allowMetaTypes = true)
|
allowMetaTypes = true)
|
||||||
result = liftingWalk(expanded, true)
|
result = recurse(expanded, true)
|
||||||
|
|
||||||
of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
|
of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
|
||||||
tyAnd, tyOr, tyNot:
|
tyAnd, tyOr, tyNot:
|
||||||
result = addImplicitGeneric(copyType(paramType, getCurrOwner(c), false))
|
result = addImplicitGenericImpl(c,
|
||||||
|
copyType(paramType, getCurrOwner(c), false), paramTypId)
|
||||||
|
|
||||||
of tyGenericParam:
|
of tyGenericParam:
|
||||||
markUsed(c, paramType.sym.info, paramType.sym)
|
markUsed(c, paramType.sym.info, paramType.sym)
|
||||||
|
|
@ -1071,7 +1065,7 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
|
||||||
|
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
# result = liftingWalk(paramType)
|
# 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:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue