seminst.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements the instantiation of generic procs.
  10. # included from sem.nim
  11. proc addObjFieldsToLocalScope(c: PContext; n: PNode) =
  12. template rec(n) = addObjFieldsToLocalScope(c, n)
  13. case n.kind
  14. of nkRecList:
  15. for i in 0..<n.len:
  16. rec n[i]
  17. of nkRecCase:
  18. if n.len > 0: rec n[0]
  19. for i in 1..<n.len:
  20. if n[i].kind in {nkOfBranch, nkElse}: rec lastSon(n[i])
  21. of nkSym:
  22. let f = n.sym
  23. if f.kind == skField and fieldVisible(c, f):
  24. c.currentScope.symbols.strTableIncl(f, onConflictKeepOld=true)
  25. incl(f.flags, sfUsed)
  26. # it is not an error to shadow fields via parameters
  27. else: discard
  28. proc pushProcCon*(c: PContext; owner: PSym) =
  29. var x: PProcCon
  30. new(x)
  31. x.owner = owner
  32. x.next = c.p
  33. c.p = x
  34. const
  35. errCannotInstantiateX = "cannot instantiate: '$1'"
  36. iterator instantiateGenericParamList(c: PContext, n: PNode, pt: TIdTable): PSym =
  37. internalAssert c.config, n.kind == nkGenericParams
  38. for a in n.items:
  39. internalAssert c.config, a.kind == nkSym
  40. var q = a.sym
  41. if q.typ.kind in {tyTypeDesc, tyGenericParam, tyStatic, tyConcept}+tyTypeClasses:
  42. let symKind = if q.typ.kind == tyStatic: skConst else: skType
  43. var s = newSym(symKind, q.name, c.idgen, getCurrOwner(c), q.info)
  44. s.flags.incl {sfUsed, sfFromGeneric}
  45. var t = PType(idTableGet(pt, q.typ))
  46. if t == nil:
  47. if tfRetType in q.typ.flags:
  48. # keep the generic type and allow the return type to be bound
  49. # later by semAsgn in return type inference scenario
  50. t = q.typ
  51. else:
  52. if q.typ.kind != tyCompositeTypeClass:
  53. localError(c.config, a.info, errCannotInstantiateX % s.name.s)
  54. t = errorType(c)
  55. elif t.kind in {tyGenericParam, tyConcept}:
  56. localError(c.config, a.info, errCannotInstantiateX % q.name.s)
  57. t = errorType(c)
  58. elif isUnresolvedStatic(t) and (q.typ.kind == tyStatic or
  59. (q.typ.kind == tyGenericParam and
  60. q.typ.sons.len > 0 and
  61. q.typ.sons[0].kind == tyStatic)) and
  62. c.inGenericContext == 0 and c.matchedConcept == nil:
  63. # generic/concept type bodies will try to instantiate static values but
  64. # won't actually use them
  65. localError(c.config, a.info, errCannotInstantiateX % q.name.s)
  66. t = errorType(c)
  67. elif t.kind == tyGenericInvocation:
  68. #t = instGenericContainer(c, a, t)
  69. t = generateTypeInstance(c, pt, a, t)
  70. #t = ReplaceTypeVarsT(cl, t)
  71. s.typ = t
  72. if t.kind == tyStatic: s.ast = t.n
  73. yield s
  74. proc sameInstantiation(a, b: TInstantiation): bool =
  75. if a.concreteTypes.len == b.concreteTypes.len:
  76. for i in 0..a.concreteTypes.high:
  77. if not compareTypes(a.concreteTypes[i], b.concreteTypes[i],
  78. flags = {ExactTypeDescValues,
  79. ExactGcSafety,
  80. PickyCAliases}): return
  81. result = true
  82. proc genericCacheGet(g: ModuleGraph; genericSym: PSym, entry: TInstantiation;
  83. id: CompilesId): PSym =
  84. for inst in procInstCacheItems(g, genericSym):
  85. if (inst.compilesId == 0 or inst.compilesId == id) and sameInstantiation(entry, inst[]):
  86. return inst.sym
  87. when false:
  88. proc `$`(x: PSym): string =
  89. result = x.name.s & " " & " id " & $x.id
  90. proc freshGenSyms(c: PContext; n: PNode, owner, orig: PSym, symMap: var TIdTable) =
  91. # we need to create a fresh set of gensym'ed symbols:
  92. #if n.kind == nkSym and sfGenSym in n.sym.flags:
  93. # if n.sym.owner != orig:
  94. # echo "symbol ", n.sym.name.s, " orig ", orig, " owner ", n.sym.owner
  95. if n.kind == nkSym and sfGenSym in n.sym.flags: # and
  96. # (n.sym.owner == orig or n.sym.owner.kind in {skPackage}):
  97. let s = n.sym
  98. var x = PSym(idTableGet(symMap, s))
  99. if x != nil:
  100. n.sym = x
  101. elif s.owner == nil or s.owner.kind == skPackage:
  102. #echo "copied this ", s.name.s
  103. x = copySym(s, c.idgen)
  104. x.owner = owner
  105. idTablePut(symMap, s, x)
  106. n.sym = x
  107. else:
  108. for i in 0..<n.safeLen: freshGenSyms(c, n[i], owner, orig, symMap)
  109. proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind)
  110. proc instantiateBody(c: PContext, n, params: PNode, result, orig: PSym) =
  111. if n[bodyPos].kind != nkEmpty:
  112. let procParams = result.typ.n
  113. for i in 1..<procParams.len:
  114. addDecl(c, procParams[i].sym)
  115. maybeAddResult(c, result, result.ast)
  116. inc c.inGenericInst
  117. # add it here, so that recursive generic procs are possible:
  118. var b = n[bodyPos]
  119. var symMap: TIdTable
  120. initIdTable symMap
  121. if params != nil:
  122. for i in 1..<params.len:
  123. let param = params[i].sym
  124. if sfGenSym in param.flags:
  125. idTablePut(symMap, params[i].sym, result.typ.n[param.position+1].sym)
  126. freshGenSyms(c, b, result, orig, symMap)
  127. if sfBorrow notin orig.flags:
  128. # We do not want to generate a body for generic borrowed procs.
  129. # As body is a sym to the borrowed proc.
  130. let resultType = # todo probably refactor it into a function
  131. if result.kind == skMacro:
  132. sysTypeFromName(c.graph, n.info, "NimNode")
  133. elif not isInlineIterator(result.typ):
  134. result.typ[0]
  135. else:
  136. nil
  137. b = semProcBody(c, b, resultType)
  138. result.ast[bodyPos] = hloBody(c, b)
  139. excl(result.flags, sfForward)
  140. trackProc(c, result, result.ast[bodyPos])
  141. dec c.inGenericInst
  142. proc fixupInstantiatedSymbols(c: PContext, s: PSym) =
  143. for i in 0..<c.generics.len:
  144. if c.generics[i].genericSym.id == s.id:
  145. var oldPrc = c.generics[i].inst.sym
  146. pushProcCon(c, oldPrc)
  147. pushOwner(c, oldPrc)
  148. pushInfoContext(c.config, oldPrc.info)
  149. openScope(c)
  150. var n = oldPrc.ast
  151. n[bodyPos] = copyTree(getBody(c.graph, s))
  152. instantiateBody(c, n, oldPrc.typ.n, oldPrc, s)
  153. closeScope(c)
  154. popInfoContext(c.config)
  155. popOwner(c)
  156. popProcCon(c)
  157. proc sideEffectsCheck(c: PContext, s: PSym) =
  158. when false:
  159. if {sfNoSideEffect, sfSideEffect} * s.flags ==
  160. {sfNoSideEffect, sfSideEffect}:
  161. localError(s.info, errXhasSideEffects, s.name.s)
  162. proc instGenericContainer(c: PContext, info: TLineInfo, header: PType,
  163. allowMetaTypes = false): PType =
  164. internalAssert c.config, header.kind == tyGenericInvocation
  165. var
  166. cl: TReplTypeVars
  167. initIdTable(cl.symMap)
  168. initIdTable(cl.localCache)
  169. cl.typeMap = LayeredIdTable()
  170. initIdTable(cl.typeMap.topLayer)
  171. cl.info = info
  172. cl.c = c
  173. cl.allowMetaTypes = allowMetaTypes
  174. # We must add all generic params in scope, because the generic body
  175. # may include tyFromExpr nodes depending on these generic params.
  176. # XXX: This looks quite similar to the code in matchUserTypeClass,
  177. # perhaps the code can be extracted in a shared function.
  178. openScope(c)
  179. let genericTyp = header.base
  180. for i in 0..<genericTyp.len - 1:
  181. let genParam = genericTyp[i]
  182. var param: PSym
  183. template paramSym(kind): untyped =
  184. newSym(kind, genParam.sym.name, c.idgen, genericTyp.sym, genParam.sym.info)
  185. if genParam.kind == tyStatic:
  186. param = paramSym skConst
  187. param.ast = header[i+1].n
  188. param.typ = header[i+1]
  189. else:
  190. param = paramSym skType
  191. param.typ = makeTypeDesc(c, header[i+1])
  192. # this scope was not created by the user,
  193. # unused params shouldn't be reported.
  194. param.flags.incl sfUsed
  195. addDecl(c, param)
  196. result = replaceTypeVarsT(cl, header)
  197. closeScope(c)
  198. proc referencesAnotherParam(n: PNode, p: PSym): bool =
  199. if n.kind == nkSym:
  200. return n.sym.kind == skParam and n.sym.owner == p
  201. else:
  202. for i in 0..<n.safeLen:
  203. if referencesAnotherParam(n[i], p): return true
  204. return false
  205. proc instantiateProcType(c: PContext, pt: TIdTable,
  206. prc: PSym, info: TLineInfo) =
  207. # XXX: Instantiates a generic proc signature, while at the same
  208. # time adding the instantiated proc params into the current scope.
  209. # This is necessary, because the instantiation process may refer to
  210. # these params in situations like this:
  211. # proc foo[Container](a: Container, b: a.type.Item): typeof(b.x)
  212. #
  213. # Alas, doing this here is probably not enough, because another
  214. # proc signature could appear in the params:
  215. # proc foo[T](a: proc (x: T, b: typeof(x.y))
  216. #
  217. # The solution would be to move this logic into semtypinst, but
  218. # at this point semtypinst have to become part of sem, because it
  219. # will need to use openScope, addDecl, etc.
  220. #addDecl(c, prc)
  221. pushInfoContext(c.config, info)
  222. var typeMap = initLayeredTypeMap(pt)
  223. var cl = initTypeVars(c, typeMap, info, nil)
  224. var result = instCopyType(cl, prc.typ)
  225. let originalParams = result.n
  226. result.n = originalParams.shallowCopy
  227. for i in 1..<result.len:
  228. # twrong_field_caching requires these 'resetIdTable' calls:
  229. if i > 1:
  230. resetIdTable(cl.symMap)
  231. resetIdTable(cl.localCache)
  232. # take a note of the original type. If't a free type or static parameter
  233. # we'll need to keep it unbound for the `fitNode` operation below...
  234. var typeToFit = result[i]
  235. let needsStaticSkipping = result[i].kind == tyFromExpr
  236. result[i] = replaceTypeVarsT(cl, result[i])
  237. if needsStaticSkipping:
  238. result[i] = result[i].skipTypes({tyStatic})
  239. # ...otherwise, we use the instantiated type in `fitNode`
  240. if (typeToFit.kind != tyTypeDesc or typeToFit.base.kind != tyNone) and
  241. (typeToFit.kind != tyStatic):
  242. typeToFit = result[i]
  243. internalAssert c.config, originalParams[i].kind == nkSym
  244. let oldParam = originalParams[i].sym
  245. let param = copySym(oldParam, c.idgen)
  246. param.owner = prc
  247. param.typ = result[i]
  248. # The default value is instantiated and fitted against the final
  249. # concrete param type. We avoid calling `replaceTypeVarsN` on the
  250. # call head symbol, because this leads to infinite recursion.
  251. if oldParam.ast != nil:
  252. var def = oldParam.ast.copyTree
  253. if def.kind in nkCallKinds:
  254. for i in 1..<def.len:
  255. def[i] = replaceTypeVarsN(cl, def[i], 1)
  256. # allow symchoice since node will be fit later
  257. # although expectedType should cover it
  258. def = semExprWithType(c, def, {efAllowSymChoice}, typeToFit)
  259. if def.referencesAnotherParam(getCurrOwner(c)):
  260. def.flags.incl nfDefaultRefsParam
  261. var converted = indexTypesMatch(c, typeToFit, def.typ, def)
  262. if converted == nil:
  263. # The default value doesn't match the final instantiated type.
  264. # As an example of this, see:
  265. # https://github.com/nim-lang/Nim/issues/1201
  266. # We are replacing the default value with an error node in case
  267. # the user calls an explicit instantiation of the proc (this is
  268. # the only way the default value might be inserted).
  269. param.ast = errorNode(c, def)
  270. else:
  271. param.ast = fitNodePostMatch(c, typeToFit, converted)
  272. param.typ = result[i]
  273. result.n[i] = newSymNode(param)
  274. propagateToOwner(result, result[i])
  275. addDecl(c, param)
  276. resetIdTable(cl.symMap)
  277. resetIdTable(cl.localCache)
  278. cl.isReturnType = true
  279. result[0] = replaceTypeVarsT(cl, result[0])
  280. cl.isReturnType = false
  281. result.n[0] = originalParams[0].copyTree
  282. if result[0] != nil:
  283. propagateToOwner(result, result[0])
  284. eraseVoidParams(result)
  285. skipIntLiteralParams(result, c.idgen)
  286. prc.typ = result
  287. popInfoContext(c.config)
  288. proc fillMixinScope(c: PContext) =
  289. var p = c.p
  290. while p != nil:
  291. for bnd in p.localBindStmts:
  292. for n in bnd:
  293. addSym(c.currentScope, n.sym)
  294. p = p.next
  295. proc generateInstance(c: PContext, fn: PSym, pt: TIdTable,
  296. info: TLineInfo): PSym =
  297. ## Generates a new instance of a generic procedure.
  298. ## The `pt` parameter is a type-unsafe mapping table used to link generic
  299. ## parameters to their concrete types within the generic instance.
  300. # no need to instantiate generic templates/macros:
  301. internalAssert c.config, fn.kind notin {skMacro, skTemplate}
  302. # generates an instantiated proc
  303. if c.instCounter > 50:
  304. globalError(c.config, info, "generic instantiation too nested")
  305. inc c.instCounter
  306. let currentTypeofContext = c.inTypeofContext
  307. c.inTypeofContext = 0
  308. defer:
  309. dec c.instCounter
  310. c.inTypeofContext = currentTypeofContext
  311. # careful! we copy the whole AST including the possibly nil body!
  312. var n = copyTree(fn.ast)
  313. # NOTE: for access of private fields within generics from a different module
  314. # we set the friend module:
  315. c.friendModules.add(getModule(fn))
  316. let oldMatchedConcept = c.matchedConcept
  317. c.matchedConcept = nil
  318. let oldScope = c.currentScope
  319. while not isTopLevel(c): c.currentScope = c.currentScope.parent
  320. result = copySym(fn, c.idgen)
  321. incl(result.flags, sfFromGeneric)
  322. result.owner = fn
  323. result.ast = n
  324. pushOwner(c, result)
  325. # mixin scope:
  326. openScope(c)
  327. fillMixinScope(c)
  328. openScope(c)
  329. let gp = n[genericParamsPos]
  330. if gp.kind != nkGenericParams:
  331. # bug #22137
  332. globalError(c.config, info, "generic instantiation too nested")
  333. n[namePos] = newSymNode(result)
  334. pushInfoContext(c.config, info, fn.detailedInfo)
  335. var entry = TInstantiation.new
  336. entry.sym = result
  337. # we need to compare both the generic types and the concrete types:
  338. # generic[void](), generic[int]()
  339. # see ttypeor.nim test.
  340. var i = 0
  341. newSeq(entry.concreteTypes, fn.typ.len+gp.len-1)
  342. # let param instantiation know we are in a concept for unresolved statics:
  343. c.matchedConcept = oldMatchedConcept
  344. for s in instantiateGenericParamList(c, gp, pt):
  345. addDecl(c, s)
  346. entry.concreteTypes[i] = s.typ
  347. inc i
  348. c.matchedConcept = nil
  349. pushProcCon(c, result)
  350. instantiateProcType(c, pt, result, info)
  351. for j in 1..<result.typ.len:
  352. entry.concreteTypes[i] = result.typ[j]
  353. inc i
  354. #echo "INSTAN ", fn.name.s, " ", typeToString(result.typ), " ", entry.concreteTypes.len
  355. if tfTriggersCompileTime in result.typ.flags:
  356. incl(result.flags, sfCompileTime)
  357. n[genericParamsPos] = c.graph.emptyNode
  358. var oldPrc = genericCacheGet(c.graph, fn, entry[], c.compilesContextId)
  359. if oldPrc == nil:
  360. # we MUST not add potentially wrong instantiations to the caching mechanism.
  361. # This means recursive instantiations behave differently when in
  362. # a ``compiles`` context but this is the lesser evil. See
  363. # bug #1055 (tevilcompiles).
  364. #if c.compilesContextId == 0:
  365. entry.compilesId = c.compilesContextId
  366. addToGenericProcCache(c, fn, entry)
  367. c.generics.add(makeInstPair(fn, entry))
  368. if n[pragmasPos].kind != nkEmpty:
  369. pragma(c, result, n[pragmasPos], allRoutinePragmas)
  370. if isNil(n[bodyPos]):
  371. n[bodyPos] = copyTree(getBody(c.graph, fn))
  372. instantiateBody(c, n, fn.typ.n, result, fn)
  373. sideEffectsCheck(c, result)
  374. if result.magic notin {mSlice, mTypeOf}:
  375. # 'toOpenArray' is special and it is allowed to return 'openArray':
  376. paramsTypeCheck(c, result.typ)
  377. #echo "INSTAN ", fn.name.s, " ", typeToString(result.typ), " <-- NEW PROC!", " ", entry.concreteTypes.len
  378. else:
  379. #echo "INSTAN ", fn.name.s, " ", typeToString(result.typ), " <-- CACHED! ", typeToString(oldPrc.typ), " ", entry.concreteTypes.len
  380. result = oldPrc
  381. popProcCon(c)
  382. popInfoContext(c.config)
  383. closeScope(c) # close scope for parameters
  384. closeScope(c) # close scope for 'mixin' declarations
  385. popOwner(c)
  386. c.currentScope = oldScope
  387. discard c.friendModules.pop()
  388. c.matchedConcept = oldMatchedConcept
  389. if result.kind == skMethod: finishMethod(c, result)
  390. # inform IC of the generic
  391. #addGeneric(c.ic, result, entry.concreteTypes)