# # # The Nimrod Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # This module implements the instantiation of generic procs. # included from sem.nim proc instantiateGenericParamList(c: PContext, n: PNode, pt: TIdTable, entry: var TInstantiatedSymbol) = if n.kind != nkGenericParams: InternalError(n.info, "instantiateGenericParamList; no generic params") newSeq(entry.concreteTypes, n.len) for i in countup(0, n.len - 1): var a = n.sons[i] if a.kind != nkSym: InternalError(a.info, "instantiateGenericParamList; no symbol") var q = a.sym if q.typ.kind notin {tyTypeDesc, tyGenericParam, tyTypeClass, tyExpr}: continue var s = newSym(skType, q.name, getCurrOwner(), q.info) s.flags = s.flags + {sfUsed, sfFromGeneric} var t = PType(IdTableGet(pt, q.typ)) if t == nil: if tfRetType in q.typ.flags: # keep the generic type and allow the return type to be bound # later by semAsgn in return type inference scenario t = q.typ else: LocalError(a.info, errCannotInstantiateX, s.name.s) t = errorType(c) elif t.kind == tyGenericParam: InternalError(a.info, "instantiateGenericParamList: " & q.name.s) elif t.kind == tyGenericInvokation: #t = instGenericContainer(c, a, t) t = generateTypeInstance(c, pt, a, t) #t = ReplaceTypeVarsT(cl, t) t.flags.incl tfInstantiated s.typ = t addDecl(c, s) entry.concreteTypes[i] = t proc sameInstantiation(a, b: TInstantiatedSymbol): bool = if a.genericSym.id == b.genericSym.id and a.concreteTypes.len == b.concreteTypes.len: for i in 0 .. < a.concreteTypes.len: if not compareTypes(a.concreteTypes[i], b.concreteTypes[i], flags = {TypeDescExactMatch}): return result = true proc GenericCacheGet(c: PContext, entry: var TInstantiatedSymbol): PSym = for i in countup(0, Len(c.generics.generics) - 1): if sameInstantiation(entry, c.generics.generics[i]): result = c.generics.generics[i].instSym # checking for the concrete parameter list is wrong and unnecessary! #if equalParams(b.typ.n, instSym.typ.n) == paramsEqual: #echo "found in cache: ", getProcHeader(result) return proc removeDefaultParamValues(n: PNode) = # we remove default params, because they cannot be instantiated properly # and they are not needed anyway for instantiation (each param is already # provided). when false: for i in countup(1, sonsLen(n)-1): var a = n.sons[i] if a.kind != nkIdentDefs: IllFormedAst(a) var L = a.len if a.sons[L-1].kind != nkEmpty and a.sons[L-2].kind != nkEmpty: # ``param: typ = defaultVal``. # We don't need defaultVal for semantic checking and it's wrong for # ``cmp: proc (a, b: T): int = cmp``. Hm, for ``cmp = cmp`` that is # not possible... XXX We don't solve this issue here. a.sons[L-1] = ast.emptyNode proc freshGenSyms(n: PNode, owner: PSym, symMap: var TIdTable) = # we need to create a fresh set of gensym'ed symbols: if n.kind == nkSym and sfGenSym in n.sym.flags: var x = PSym(IdTableGet(symMap, n.sym)) if x == nil: x = copySym(n.sym, false) x.owner = owner IdTablePut(symMap, n.sym, x) n.sym = x else: for i in 0 .. 1000: InternalError(fn.ast.info, "nesting too deep") inc(c.InstCounter) # careful! we copy the whole AST including the possibly nil body! var n = copyTree(fn.ast) # NOTE: for access of private fields within generics from a different module # we set the friend module: var oldFriend = c.friendModule c.friendModule = getModule(fn) result = copySym(fn, false) incl(result.flags, sfFromGeneric) # keep the owner if it's an inner proc (for proper closure transformations): if fn.owner.kind == skModule: result.owner = getCurrOwner().owner result.ast = n pushOwner(result) openScope(c.tab) if n.sons[genericParamsPos].kind == nkEmpty: InternalError(n.info, "generateInstance") n.sons[namePos] = newSymNode(result) pushInfoContext(info) var entry: TInstantiatedSymbol entry.instSym = result entry.genericSym = fn instantiateGenericParamList(c, n.sons[genericParamsPos], pt, entry) n.sons[genericParamsPos] = ast.emptyNode # semantic checking for the parameters: if n.sons[paramsPos].kind != nkEmpty: removeDefaultParamValues(n.sons[ParamsPos]) semParamList(c, n.sons[ParamsPos], nil, result) else: result.typ = newTypeS(tyProc, c) rawAddSon(result.typ, nil) result.typ.callConv = fn.typ.callConv var oldPrc = GenericCacheGet(c, entry) if oldPrc == nil: c.generics.generics.add(entry) if n.sons[pragmasPos].kind != nkEmpty: pragma(c, result, n.sons[pragmasPos], allRoutinePragmas) if isNil(n.sons[bodyPos]): n.sons[bodyPos] = copyTree(fn.getBody) if fn.kind != skTemplate: instantiateBody(c, n, result) sideEffectsCheck(c, result) ParamsTypeCheck(c, result.typ) else: result = oldPrc popInfoContext() closeScope(c.tab) # close scope for parameters popOwner() c.friendModule = oldFriend dec(c.InstCounter) proc instGenericContainer(c: PContext, n: PNode, header: PType): PType = var cl: TReplTypeVars InitIdTable(cl.symMap) InitIdTable(cl.typeMap) cl.info = n.info cl.c = c result = ReplaceTypeVarsT(cl, header)