diff options
author | Araq <rumpf_a@web.de> | 2011-02-21 20:06:34 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-02-21 20:06:34 +0100 |
commit | fdde4d3a9266e8a393f0303b1982a2dc9ca7ca6e (patch) | |
tree | a076ec612bb98d6dd1a8eff18505f2b3c90610f7 /rod/seminst.nim | |
parent | 4e7a22cac31a0a703c080c7fe0d2569f56ab16a0 (diff) | |
download | Nim-fdde4d3a9266e8a393f0303b1982a2dc9ca7ca6e.tar.gz |
refactoring: suggest can import sigmatch for type matching
Diffstat (limited to 'rod/seminst.nim')
-rwxr-xr-x | rod/seminst.nim | 138 |
1 files changed, 2 insertions, 136 deletions
diff --git a/rod/seminst.nim b/rod/seminst.nim index f2acdba60..d32aff15e 100755 --- a/rod/seminst.nim +++ b/rod/seminst.nim @@ -7,35 +7,11 @@ # distribution, for details about the copyright. # -# This module does the instantiation of generic procs and types. +# This module does the instantiation of generic procs. proc generateInstance(c: PContext, fn: PSym, pt: TIdTable, info: TLineInfo): PSym # generates an instantiated proc -proc searchInstTypes(tab: TIdTable, key: PType): PType = - # returns nil if we need to declare this type - result = PType(IdTableGet(tab, key)) - if (result == nil) and (tab.counter > 0): - # we have to do a slow linear search because types may need - # to be compared by their structure: - for h in countup(0, high(tab.data)): - var t = PType(tab.data[h].key) - if t != nil: - if key.containerId == t.containerID: - var match = true - for j in countup(0, sonsLen(t) - 1): - # XXX sameType is not really correct for nested generics? - if not sameType(t.sons[j], key.sons[j]): - match = false - break - if match: - return PType(tab.data[h].val) - -proc containsGenericTypeIter(t: PType, closure: PObject): bool = - result = t.kind in GenericTypes - -proc containsGenericType(t: PType): bool = - result = iterOverType(t, containsGenericTypeIter, nil) proc instantiateGenericParamList(c: PContext, n: PNode, pt: TIdTable) = if (n.kind != nkGenericParams): @@ -129,106 +105,7 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable, c.p = oldP # restore c.module = oldMod dec(c.InstCounter) - -proc checkConstructedType(info: TLineInfo, t: PType) = - if (tfAcyclic in t.flags) and (skipTypes(t, abstractInst).kind != tyObject): - LocalError(info, errInvalidPragmaX, "acyclic") - elif computeSize(t) < 0: - LocalError(info, errIllegalRecursionInTypeX, typeToString(t)) - elif (t.kind == tyVar) and (t.sons[0].kind == tyVar): - LocalError(info, errVarVarTypeNotAllowed) - -type - TReplTypeVars{.final.} = object - c*: PContext - typeMap*: TIdTable # map PType to PType - symMap*: TIdTable # map PSym to PSym - info*: TLineInfo - - -proc ReplaceTypeVarsT(cl: var TReplTypeVars, t: PType): PType -proc ReplaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym -proc ReplaceTypeVarsN(cl: var TReplTypeVars, n: PNode): PNode = - if n != nil: - result = copyNode(n) - result.typ = ReplaceTypeVarsT(cl, n.typ) - case n.kind - of nkNone..pred(nkSym), succ(nkSym)..nkNilLit: - nil - of nkSym: - result.sym = ReplaceTypeVarsS(cl, n.sym) - else: - var length = sonsLen(n) - if length > 0: - newSons(result, length) - for i in countup(0, length - 1): - result.sons[i] = ReplaceTypeVarsN(cl, n.sons[i]) - -proc ReplaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym = - if s == nil: return nil - result = PSym(idTableGet(cl.symMap, s)) - if result == nil: - result = copySym(s, false) - incl(result.flags, sfFromGeneric) - idTablePut(cl.symMap, s, result) - result.typ = ReplaceTypeVarsT(cl, s.typ) - result.owner = s.owner - result.ast = ReplaceTypeVarsN(cl, s.ast) - -proc lookupTypeVar(cl: TReplTypeVars, t: PType): PType = - result = PType(idTableGet(cl.typeMap, t)) - if result == nil: - GlobalError(t.sym.info, errCannotInstantiateX, typeToString(t)) - elif result.kind == tyGenericParam: - InternalError(cl.info, "substitution with generic parameter") - -proc ReplaceTypeVarsT(cl: var TReplTypeVars, t: PType): PType = - var body, newbody, x, header: PType - result = t - if t == nil: return - case t.kind - of tyGenericParam: - result = lookupTypeVar(cl, t) - of tyGenericInvokation: - body = t.sons[0] - if body.kind != tyGenericBody: InternalError(cl.info, "no generic body") - header = nil - for i in countup(1, sonsLen(t) - 1): - if t.sons[i].kind == tyGenericParam: - x = lookupTypeVar(cl, t.sons[i]) - if header == nil: header = copyType(t, t.owner, false) - header.sons[i] = x - else: - x = t.sons[i] - idTablePut(cl.typeMap, body.sons[i - 1], x) - if header == nil: header = t - result = searchInstTypes(gInstTypes, header) - if result != nil: return - result = newType(tyGenericInst, t.sons[0].owner) - for i in countup(0, sonsLen(t) - 1): - # if one of the params is not concrete, we cannot do anything - # but we already raised an error! - addSon(result, header.sons[i]) - idTablePut(gInstTypes, header, result) - newbody = ReplaceTypeVarsT(cl, lastSon(body)) - newbody.n = ReplaceTypeVarsN(cl, lastSon(body).n) - addSon(result, newbody) - #writeln(output, ropeToStr(Typetoyaml(newbody))); - checkConstructedType(cl.info, newbody) - of tyGenericBody: - InternalError(cl.info, "ReplaceTypeVarsT: tyGenericBody") - result = ReplaceTypeVarsT(cl, lastSon(t)) - else: - if containsGenericType(t): - result = copyType(t, t.owner, false) - for i in countup(0, sonsLen(result) - 1): - result.sons[i] = ReplaceTypeVarsT(cl, result.sons[i]) - result.n = ReplaceTypeVarsN(cl, result.n) - if result.Kind in GenericTypes: - LocalError(cl.info, errCannotInstantiateX, TypeToString(t, preferName)) - #writeln(output, ropeToStr(Typetoyaml(result))) - #checkConstructedType(cl.info, result) - + proc instGenericContainer(c: PContext, n: PNode, header: PType): PType = var cl: TReplTypeVars InitIdTable(cl.symMap) @@ -237,14 +114,3 @@ proc instGenericContainer(c: PContext, n: PNode, header: PType): PType = cl.c = c result = ReplaceTypeVarsT(cl, header) -proc generateTypeInstance(p: PContext, pt: TIdTable, arg: PNode, - t: PType): PType = - var cl: TReplTypeVars - InitIdTable(cl.symMap) - copyIdTable(cl.typeMap, pt) - cl.info = arg.info - cl.c = p - pushInfoContext(arg.info) - result = ReplaceTypeVarsT(cl, t) - popInfoContext() - |