From a27eb51535f9ff233b67e5bac80cc51b81c343c7 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 27 Dec 2013 18:34:28 +0200 Subject: towards support for composite type classes such as seq[Number] and SquareMatrix[T] --- compiler/semtypinst.nim | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'compiler/semtypinst.nim') diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim index 69d91766b..f7750171d 100644 --- a/compiler/semtypinst.nim +++ b/compiler/semtypinst.nim @@ -50,9 +50,10 @@ proc searchInstTypes*(key: PType): PType = block MatchType: for j in 1 .. high(key.sons): # XXX sameType is not really correct for nested generics? - if not sameType(inst.sons[j], key.sons[j]): + if not compareTypes(inst.sons[j], key.sons[j], + flags = {ExactGenericParams}): break MatchType - + return inst proc cacheTypeInst*(inst: PType) = @@ -67,6 +68,8 @@ type typeMap*: TIdTable # map PType to PType symMap*: TIdTable # map PSym to PSym info*: TLineInfo + allowMetaTypes*: bool # allow types such as seq[Number] + # i.e. the result contains unresolved generics proc ReplaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType proc ReplaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym @@ -132,9 +135,10 @@ proc ReplaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym = proc lookupTypeVar(cl: TReplTypeVars, t: PType): PType = result = PType(idTableGet(cl.typeMap, t)) if result == nil: + if cl.allowMetaTypes: return LocalError(t.sym.info, errCannotInstantiateX, typeToString(t)) result = errorType(cl.c) - elif result.kind == tyGenericParam: + elif result.kind == tyGenericParam and not cl.allowMetaTypes: InternalError(cl.info, "substitution with generic parameter") proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType = @@ -150,11 +154,11 @@ proc handleGenericInvokation(cl: var TReplTypeVars, t: PType): PType = var x = t.sons[i] if x.kind == tyGenericParam: x = lookupTypeVar(cl, x) - if header == nil: header = copyType(t, t.owner, false) - header.sons[i] = x - propagateToOwner(header, x) - #idTablePut(cl.typeMap, body.sons[i-1], x) - + if x != nil: + if header == nil: header = copyType(t, t.owner, false) + header.sons[i] = x + propagateToOwner(header, x) + if header != nil: # search again after first pass: result = searchInstTypes(header) @@ -202,6 +206,7 @@ proc ReplaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType = of tyTypeClass: nil of tyGenericParam: result = lookupTypeVar(cl, t) + if result == nil: return t if result.kind == tyGenericInvokation: result = handleGenericInvokation(cl, result) of tyGenericInvokation: -- cgit 1.4.1-2-gfad0