semtypinst.nim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module does the instantiation of generic types.
  10. import ast, astalgo, msgs, types, magicsys, semdata, renderer, options,
  11. lineinfos, modulegraphs
  12. when defined(nimPreviewSlimSystem):
  13. import std/assertions
  14. const tfInstClearedFlags = {tfHasMeta, tfUnresolved}
  15. proc checkPartialConstructedType(conf: ConfigRef; info: TLineInfo, t: PType) =
  16. if t.kind in {tyVar, tyLent} and t[0].kind in {tyVar, tyLent}:
  17. localError(conf, info, "type 'var var' is not allowed")
  18. proc checkConstructedType*(conf: ConfigRef; info: TLineInfo, typ: PType) =
  19. var t = typ.skipTypes({tyDistinct})
  20. if t.kind in tyTypeClasses: discard
  21. elif t.kind in {tyVar, tyLent} and t[0].kind in {tyVar, tyLent}:
  22. localError(conf, info, "type 'var var' is not allowed")
  23. elif computeSize(conf, t) == szIllegalRecursion or isTupleRecursive(t):
  24. localError(conf, info, "illegal recursion in type '" & typeToString(t) & "'")
  25. when false:
  26. if t.kind == tyObject and t[0] != nil:
  27. if t[0].kind != tyObject or tfFinal in t[0].flags:
  28. localError(info, errInheritanceOnlyWithNonFinalObjects)
  29. proc searchInstTypes*(g: ModuleGraph; key: PType): PType =
  30. result = nil
  31. let genericTyp = key[0]
  32. if not (genericTyp.kind == tyGenericBody and
  33. genericTyp.sym != nil): return
  34. for inst in typeInstCacheItems(g, genericTyp.sym):
  35. if inst.id == key.id: return inst
  36. if inst.len < key.len:
  37. # XXX: This happens for prematurely cached
  38. # types such as Channel[empty]. Why?
  39. # See the notes for PActor in handleGenericInvocation
  40. # if this is return the same type gets cached more than it needs to
  41. continue
  42. if not sameFlags(inst, key):
  43. continue
  44. block matchType:
  45. for j in 1..<len(key):
  46. # XXX sameType is not really correct for nested generics?
  47. if not compareTypes(inst[j], key[j],
  48. flags = {ExactGenericParams, PickyCAliases}):
  49. break matchType
  50. return inst
  51. proc cacheTypeInst(c: PContext; inst: PType) =
  52. let gt = inst[0]
  53. let t = if gt.kind == tyGenericBody: gt.lastSon else: gt
  54. if t.kind in {tyStatic, tyError, tyGenericParam} + tyTypeClasses:
  55. return
  56. addToGenericCache(c, gt.sym, inst)
  57. type
  58. LayeredIdTable* {.acyclic.} = ref object
  59. topLayer*: TIdTable
  60. nextLayer*: LayeredIdTable
  61. TReplTypeVars* = object
  62. c*: PContext
  63. typeMap*: LayeredIdTable # map PType to PType
  64. symMap*: TIdTable # map PSym to PSym
  65. localCache*: TIdTable # local cache for remembering already replaced
  66. # types during instantiation of meta types
  67. # (they are not stored in the global cache)
  68. info*: TLineInfo
  69. allowMetaTypes*: bool # allow types such as seq[Number]
  70. # i.e. the result contains unresolved generics
  71. skipTypedesc*: bool # whether we should skip typeDescs
  72. isReturnType*: bool
  73. owner*: PSym # where this instantiation comes from
  74. recursionLimit: int
  75. proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType
  76. proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym
  77. proc replaceTypeVarsN*(cl: var TReplTypeVars, n: PNode; start=0; expectedType: PType = nil): PNode
  78. proc initLayeredTypeMap*(pt: TIdTable): LayeredIdTable =
  79. result = LayeredIdTable()
  80. copyIdTable(result.topLayer, pt)
  81. proc newTypeMapLayer*(cl: var TReplTypeVars): LayeredIdTable =
  82. result = LayeredIdTable(nextLayer: cl.typeMap, topLayer: initIdTable())
  83. proc lookup(typeMap: LayeredIdTable, key: PType): PType =
  84. result = nil
  85. var tm = typeMap
  86. while tm != nil:
  87. result = PType(idTableGet(tm.topLayer, key))
  88. if result != nil: return
  89. tm = tm.nextLayer
  90. template put(typeMap: LayeredIdTable, key, value: PType) =
  91. idTablePut(typeMap.topLayer, key, value)
  92. template checkMetaInvariants(cl: TReplTypeVars, t: PType) = # noop code
  93. when false:
  94. if t != nil and tfHasMeta in t.flags and
  95. cl.allowMetaTypes == false:
  96. echo "UNEXPECTED META ", t.id, " ", instantiationInfo(-1)
  97. debug t
  98. writeStackTrace()
  99. proc replaceTypeVarsT*(cl: var TReplTypeVars, t: PType): PType =
  100. result = replaceTypeVarsTAux(cl, t)
  101. checkMetaInvariants(cl, result)
  102. proc prepareNode(cl: var TReplTypeVars, n: PNode): PNode =
  103. let t = replaceTypeVarsT(cl, n.typ)
  104. if t != nil and t.kind == tyStatic and t.n != nil:
  105. return if tfUnresolved in t.flags: prepareNode(cl, t.n)
  106. else: t.n
  107. result = copyNode(n)
  108. result.typ = t
  109. if result.kind == nkSym: result.sym = replaceTypeVarsS(cl, n.sym)
  110. let isCall = result.kind in nkCallKinds
  111. for i in 0..<n.safeLen:
  112. # XXX HACK: ``f(a, b)``, avoid to instantiate `f`
  113. if isCall and i == 0: result.add(n[i])
  114. else: result.add(prepareNode(cl, n[i]))
  115. proc isTypeParam(n: PNode): bool =
  116. # XXX: generic params should use skGenericParam instead of skType
  117. return n.kind == nkSym and
  118. (n.sym.kind == skGenericParam or
  119. (n.sym.kind == skType and sfFromGeneric in n.sym.flags))
  120. when false: # old workaround
  121. proc reResolveCallsWithTypedescParams(cl: var TReplTypeVars, n: PNode): PNode =
  122. # This is needed for tuninstantiatedgenericcalls
  123. # It's possible that a generic param will be used in a proc call to a
  124. # typedesc accepting proc. After generic param substitution, such procs
  125. # should be optionally instantiated with the correct type. In order to
  126. # perform this instantiation, we need to re-run the generateInstance path
  127. # in the compiler, but it's quite complicated to do so at the moment so we
  128. # resort to a mild hack; the head symbol of the call is temporary reset and
  129. # overload resolution is executed again (which may trigger generateInstance).
  130. if n.kind in nkCallKinds and sfFromGeneric in n[0].sym.flags:
  131. var needsFixing = false
  132. for i in 1..<n.safeLen:
  133. if isTypeParam(n[i]): needsFixing = true
  134. if needsFixing:
  135. n[0] = newSymNode(n[0].sym.owner)
  136. return cl.c.semOverloadedCall(cl.c, n, n, {skProc, skFunc}, {})
  137. for i in 0..<n.safeLen:
  138. n[i] = reResolveCallsWithTypedescParams(cl, n[i])
  139. return n
  140. proc replaceObjBranches(cl: TReplTypeVars, n: PNode): PNode =
  141. result = n
  142. case n.kind
  143. of nkNone..nkNilLit:
  144. discard
  145. of nkRecWhen:
  146. var branch: PNode = nil # the branch to take
  147. for i in 0..<n.len:
  148. var it = n[i]
  149. if it == nil: illFormedAst(n, cl.c.config)
  150. case it.kind
  151. of nkElifBranch:
  152. checkSonsLen(it, 2, cl.c.config)
  153. var cond = it[0]
  154. var e = cl.c.semConstExpr(cl.c, cond)
  155. if e.kind != nkIntLit:
  156. internalError(cl.c.config, e.info, "ReplaceTypeVarsN: when condition not a bool")
  157. if e.intVal != 0 and branch == nil: branch = it[1]
  158. of nkElse:
  159. checkSonsLen(it, 1, cl.c.config)
  160. if branch == nil: branch = it[0]
  161. else: illFormedAst(n, cl.c.config)
  162. if branch != nil:
  163. result = replaceObjBranches(cl, branch)
  164. else:
  165. result = newNodeI(nkRecList, n.info)
  166. else:
  167. for i in 0..<n.len:
  168. n[i] = replaceObjBranches(cl, n[i])
  169. proc hasValuelessStatics(n: PNode): bool =
  170. # We should only attempt to call an expression that has no tyStatics
  171. # As those are unresolved generic parameters, which means in the following
  172. # The compiler attempts to do `T == 300` which errors since the typeclass `MyThing` lacks a parameter
  173. #[
  174. type MyThing[T: static int] = object
  175. when T == 300:
  176. a
  177. proc doThing(_: MyThing)
  178. ]#
  179. if n.safeLen == 0 and n.kind != nkEmpty: # Some empty nodes can get in here
  180. n.typ == nil or n.typ.kind == tyStatic
  181. else:
  182. for x in n:
  183. if hasValuelessStatics(x):
  184. return true
  185. false
  186. proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode; start=0; expectedType: PType = nil): PNode =
  187. if n == nil: return
  188. result = copyNode(n)
  189. if n.typ != nil:
  190. result.typ = replaceTypeVarsT(cl, n.typ)
  191. checkMetaInvariants(cl, result.typ)
  192. case n.kind
  193. of nkNone..pred(nkSym), succ(nkSym)..nkNilLit:
  194. discard
  195. of nkOpenSymChoice, nkClosedSymChoice: result = n
  196. of nkSym:
  197. result.sym = replaceTypeVarsS(cl, n.sym)
  198. if result.sym.typ.kind == tyVoid:
  199. # don't add the 'void' field
  200. result = newNodeI(nkRecList, n.info)
  201. of nkRecWhen:
  202. var branch: PNode = nil # the branch to take
  203. for i in 0..<n.len:
  204. var it = n[i]
  205. if it == nil: illFormedAst(n, cl.c.config)
  206. case it.kind
  207. of nkElifBranch:
  208. checkSonsLen(it, 2, cl.c.config)
  209. var cond = prepareNode(cl, it[0])
  210. if not cond.hasValuelessStatics:
  211. var e = cl.c.semConstExpr(cl.c, cond)
  212. if e.kind != nkIntLit:
  213. internalError(cl.c.config, e.info, "ReplaceTypeVarsN: when condition not a bool")
  214. if e.intVal != 0 and branch == nil: branch = it[1]
  215. of nkElse:
  216. checkSonsLen(it, 1, cl.c.config)
  217. if branch == nil: branch = it[0]
  218. else: illFormedAst(n, cl.c.config)
  219. if branch != nil:
  220. result = replaceTypeVarsN(cl, branch)
  221. else:
  222. result = newNodeI(nkRecList, n.info)
  223. of nkStaticExpr:
  224. var n = prepareNode(cl, n)
  225. when false:
  226. n = reResolveCallsWithTypedescParams(cl, n)
  227. result = if cl.allowMetaTypes: n
  228. else: cl.c.semExpr(cl.c, n, {}, expectedType)
  229. if not cl.allowMetaTypes and expectedType != nil:
  230. assert result.kind notin nkCallKinds
  231. else:
  232. if n.len > 0:
  233. newSons(result, n.len)
  234. if start > 0:
  235. result[0] = n[0]
  236. for i in start..<n.len:
  237. result[i] = replaceTypeVarsN(cl, n[i])
  238. proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym =
  239. if s == nil: return nil
  240. # symbol is not our business:
  241. if cl.owner != nil and s.owner != cl.owner:
  242. return s
  243. # XXX: Bound symbols in default parameter expressions may reach here.
  244. # We cannot process them, because `sym.n` may point to a proc body with
  245. # cyclic references that will lead to an infinite recursion.
  246. # Perhaps we should not use a black-list here, but a whitelist instead
  247. # (e.g. skGenericParam and skType).
  248. # Note: `s.magic` may be `mType` in an example such as:
  249. # proc foo[T](a: T, b = myDefault(type(a)))
  250. if s.kind in routineKinds+{skLet, skConst, skVar} or s.magic != mNone:
  251. return s
  252. #result = PSym(idTableGet(cl.symMap, s))
  253. #if result == nil:
  254. #[
  255. We cannot naively check for symbol recursions, because otherwise
  256. object types A, B whould share their fields!
  257. import tables
  258. type
  259. Table[S, T] = object
  260. x: S
  261. y: T
  262. G[T] = object
  263. inodes: Table[int, T] # A
  264. rnodes: Table[T, int] # B
  265. var g: G[string]
  266. ]#
  267. result = copySym(s, cl.c.idgen)
  268. incl(result.flags, sfFromGeneric)
  269. #idTablePut(cl.symMap, s, result)
  270. result.owner = s.owner
  271. result.typ = replaceTypeVarsT(cl, s.typ)
  272. if result.kind != skType:
  273. result.ast = replaceTypeVarsN(cl, s.ast)
  274. proc lookupTypeVar(cl: var TReplTypeVars, t: PType): PType =
  275. result = cl.typeMap.lookup(t)
  276. if result == nil:
  277. if cl.allowMetaTypes or tfRetType in t.flags: return
  278. localError(cl.c.config, t.sym.info, "cannot instantiate: '" & typeToString(t) & "'")
  279. result = errorType(cl.c)
  280. # In order to prevent endless recursions, we must remember
  281. # this bad lookup and replace it with errorType everywhere.
  282. # These code paths are only active in "nim check"
  283. cl.typeMap.put(t, result)
  284. elif result.kind == tyGenericParam and not cl.allowMetaTypes:
  285. internalError(cl.c.config, cl.info, "substitution with generic parameter")
  286. proc instCopyType*(cl: var TReplTypeVars, t: PType): PType =
  287. # XXX: relying on allowMetaTypes is a kludge
  288. if cl.allowMetaTypes:
  289. result = t.exactReplica
  290. else:
  291. result = copyType(t, nextTypeId(cl.c.idgen), t.owner)
  292. copyTypeProps(cl.c.graph, cl.c.idgen.module, result, t)
  293. #cl.typeMap.topLayer.idTablePut(result, t)
  294. if cl.allowMetaTypes: return
  295. result.flags.incl tfFromGeneric
  296. if not (t.kind in tyMetaTypes or
  297. (t.kind == tyStatic and t.n == nil)):
  298. result.flags.excl tfInstClearedFlags
  299. else:
  300. result.flags.excl tfHasAsgn
  301. when false:
  302. if newDestructors:
  303. result.assignment = nil
  304. result.destructor = nil
  305. result.sink = nil
  306. proc handleGenericInvocation(cl: var TReplTypeVars, t: PType): PType =
  307. # tyGenericInvocation[A, tyGenericInvocation[A, B]]
  308. # is difficult to handle:
  309. var body = t[0]
  310. if body.kind != tyGenericBody:
  311. internalError(cl.c.config, cl.info, "no generic body")
  312. var header = t
  313. # search for some instantiation here:
  314. if cl.allowMetaTypes:
  315. result = PType(idTableGet(cl.localCache, t))
  316. else:
  317. result = searchInstTypes(cl.c.graph, t)
  318. if result != nil and sameFlags(result, t):
  319. when defined(reportCacheHits):
  320. echo "Generic instantiation cached ", typeToString(result), " for ", typeToString(t)
  321. return
  322. for i in 1..<t.len:
  323. var x = t[i]
  324. if x.kind in {tyGenericParam}:
  325. x = lookupTypeVar(cl, x)
  326. if x != nil:
  327. if header == t: header = instCopyType(cl, t)
  328. header[i] = x
  329. propagateToOwner(header, x)
  330. else:
  331. propagateToOwner(header, x)
  332. if header != t:
  333. # search again after first pass:
  334. result = searchInstTypes(cl.c.graph, header)
  335. if result != nil and sameFlags(result, t):
  336. when defined(reportCacheHits):
  337. echo "Generic instantiation cached ", typeToString(result), " for ",
  338. typeToString(t), " header ", typeToString(header)
  339. return
  340. else:
  341. header = instCopyType(cl, t)
  342. result = newType(tyGenericInst, nextTypeId(cl.c.idgen), t[0].owner, sons = @[header[0]])
  343. result.flags = header.flags
  344. # be careful not to propagate unnecessary flags here (don't use rawAddSon)
  345. # ugh need another pass for deeply recursive generic types (e.g. PActor)
  346. # we need to add the candidate here, before it's fully instantiated for
  347. # recursive instantions:
  348. if not cl.allowMetaTypes:
  349. cacheTypeInst(cl.c, result)
  350. else:
  351. idTablePut(cl.localCache, t, result)
  352. let oldSkipTypedesc = cl.skipTypedesc
  353. cl.skipTypedesc = true
  354. cl.typeMap = newTypeMapLayer(cl)
  355. for i in 1..<t.len:
  356. var x = replaceTypeVarsT(cl):
  357. if header[i].kind == tyGenericInst:
  358. t[i]
  359. else:
  360. header[i]
  361. assert x.kind != tyGenericInvocation
  362. header[i] = x
  363. propagateToOwner(header, x)
  364. cl.typeMap.put(body[i-1], x)
  365. for i in 1..<t.len:
  366. # if one of the params is not concrete, we cannot do anything
  367. # but we already raised an error!
  368. rawAddSon(result, header[i], propagateHasAsgn = false)
  369. if body.kind == tyError:
  370. return
  371. let bbody = lastSon body
  372. var newbody = replaceTypeVarsT(cl, bbody)
  373. cl.skipTypedesc = oldSkipTypedesc
  374. newbody.flags = newbody.flags + (t.flags + body.flags - tfInstClearedFlags)
  375. result.flags = result.flags + newbody.flags - tfInstClearedFlags
  376. cl.typeMap = cl.typeMap.nextLayer
  377. # This is actually wrong: tgeneric_closure fails with this line:
  378. #newbody.callConv = body.callConv
  379. # This type may be a generic alias and we want to resolve it here.
  380. # One step is enough, because the recursive nature of
  381. # handleGenericInvocation will handle the alias-to-alias-to-alias case
  382. if newbody.isGenericAlias: newbody = newbody.skipGenericAlias
  383. rawAddSon(result, newbody)
  384. checkPartialConstructedType(cl.c.config, cl.info, newbody)
  385. if not cl.allowMetaTypes:
  386. let dc = cl.c.graph.getAttachedOp(newbody, attachedDeepCopy)
  387. if dc != nil and sfFromGeneric notin dc.flags:
  388. # 'deepCopy' needs to be instantiated for
  389. # generics *when the type is constructed*:
  390. cl.c.graph.setAttachedOp(cl.c.module.position, newbody, attachedDeepCopy,
  391. cl.c.instTypeBoundOp(cl.c, dc, result, cl.info, attachedDeepCopy, 1))
  392. if newbody.typeInst == nil:
  393. # doAssert newbody.typeInst == nil
  394. newbody.typeInst = result
  395. if tfRefsAnonObj in newbody.flags and newbody.kind != tyGenericInst:
  396. # can come here for tyGenericInst too, see tests/metatype/ttypeor.nim
  397. # need to look into this issue later
  398. assert newbody.kind in {tyRef, tyPtr}
  399. if newbody.lastSon.typeInst != nil:
  400. #internalError(cl.c.config, cl.info, "ref already has a 'typeInst' field")
  401. discard
  402. else:
  403. newbody.lastSon.typeInst = result
  404. # DESTROY: adding object|opt for opt[topttree.Tree]
  405. # sigmatch: Formal opt[=destroy.T] real opt[topttree.Tree]
  406. # adding myseq for myseq[system.int]
  407. # sigmatch: Formal myseq[=destroy.T] real myseq[system.int]
  408. #echo "DESTROY: adding ", typeToString(newbody), " for ", typeToString(result, preferDesc)
  409. let mm = skipTypes(bbody, abstractPtrs)
  410. if tfFromGeneric notin mm.flags:
  411. # bug #5479, prevent endless recursions here:
  412. incl mm.flags, tfFromGeneric
  413. for col, meth in methodsForGeneric(cl.c.graph, mm):
  414. # we instantiate the known methods belonging to that type, this causes
  415. # them to be registered and that's enough, so we 'discard' the result.
  416. discard cl.c.instTypeBoundOp(cl.c, meth, result, cl.info,
  417. attachedAsgn, col)
  418. excl mm.flags, tfFromGeneric
  419. proc eraseVoidParams*(t: PType) =
  420. # transform '(): void' into '()' because old parts of the compiler really
  421. # don't deal with '(): void':
  422. if t[0] != nil and t[0].kind == tyVoid:
  423. t[0] = nil
  424. for i in 1..<t.len:
  425. # don't touch any memory unless necessary
  426. if t[i].kind == tyVoid:
  427. var pos = i
  428. for j in i+1..<t.len:
  429. if t[j].kind != tyVoid:
  430. t[pos] = t[j]
  431. t.n[pos] = t.n[j]
  432. inc pos
  433. newSons t, pos
  434. setLen t.n.sons, pos
  435. break
  436. proc skipIntLiteralParams*(t: PType; idgen: IdGenerator) =
  437. for i in 0..<t.len:
  438. let p = t[i]
  439. if p == nil: continue
  440. let skipped = p.skipIntLit(idgen)
  441. if skipped != p:
  442. t[i] = skipped
  443. if i > 0: t.n[i].sym.typ = skipped
  444. # when the typeof operator is used on a static input
  445. # param, the results gets infected with static as well:
  446. if t[0] != nil and t[0].kind == tyStatic:
  447. t[0] = t[0].base
  448. proc propagateFieldFlags(t: PType, n: PNode) =
  449. # This is meant for objects and tuples
  450. # The type must be fully instantiated!
  451. if n.isNil:
  452. return
  453. #internalAssert n.kind != nkRecWhen
  454. case n.kind
  455. of nkSym:
  456. propagateToOwner(t, n.sym.typ)
  457. of nkRecList, nkRecCase, nkOfBranch, nkElse:
  458. for son in n:
  459. propagateFieldFlags(t, son)
  460. else: discard
  461. proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
  462. template bailout =
  463. if (t.sym == nil) or (t.sym != nil and sfGeneratedType in t.sym.flags):
  464. # In the first case 't.sym' can be 'nil' if the type is a ref/ptr, see
  465. # issue https://github.com/nim-lang/Nim/issues/20416 for more details.
  466. # Fortunately for us this works for now because partial ref/ptr types are
  467. # not allowed in object construction, eg.
  468. # type
  469. # Container[T] = ...
  470. # O = object
  471. # val: ref Container
  472. #
  473. # In the second case only consider the recursion limit if the symbol is a
  474. # type with generic parameters that have not been explicitly supplied,
  475. # typechecking should terminate when generic parameters are explicitly
  476. # supplied.
  477. if cl.recursionLimit > 100:
  478. # bail out, see bug #2509. But note this caching is in general wrong,
  479. # look at this example where TwoVectors should not share the generic
  480. # instantiations (bug #3112):
  481. # type
  482. # Vector[N: static[int]] = array[N, float64]
  483. # TwoVectors[Na, Nb: static[int]] = (Vector[Na], Vector[Nb])
  484. result = PType(idTableGet(cl.localCache, t))
  485. if result != nil: return result
  486. inc cl.recursionLimit
  487. result = t
  488. if t == nil: return
  489. if t.kind in {tyStatic, tyGenericParam, tyConcept} + tyTypeClasses:
  490. let lookup = cl.typeMap.lookup(t)
  491. if lookup != nil: return lookup
  492. case t.kind
  493. of tyGenericInvocation:
  494. result = handleGenericInvocation(cl, t)
  495. if result.lastSon.kind == tyUserTypeClass:
  496. result.kind = tyUserTypeClassInst
  497. of tyGenericBody:
  498. localError(
  499. cl.c.config,
  500. cl.info,
  501. "cannot instantiate: '" &
  502. typeToString(t, preferDesc) &
  503. "'; Maybe generic arguments are missing?")
  504. result = errorType(cl.c)
  505. #result = replaceTypeVarsT(cl, lastSon(t))
  506. of tyFromExpr:
  507. if cl.allowMetaTypes: return
  508. # This assert is triggered when a tyFromExpr was created in a cyclic
  509. # way. You should break the cycle at the point of creation by introducing
  510. # a call such as: `n.typ = makeTypeFromExpr(c, n.copyTree)`
  511. # Otherwise, the cycle will be fatal for the prepareNode call below
  512. assert t.n.typ != t
  513. var n = prepareNode(cl, t.n)
  514. if n.kind != nkEmpty:
  515. n = cl.c.semConstExpr(cl.c, n)
  516. if n.typ.kind == tyTypeDesc:
  517. # XXX: sometimes, chained typedescs enter here.
  518. # It may be worth investigating why this is happening,
  519. # because it may cause other bugs elsewhere.
  520. result = n.typ.skipTypes({tyTypeDesc})
  521. # result = n.typ.base
  522. else:
  523. if n.typ.kind != tyStatic and n.kind != nkType:
  524. # XXX: In the future, semConstExpr should
  525. # return tyStatic values to let anyone make
  526. # use of this knowledge. The patching here
  527. # won't be necessary then.
  528. result = newTypeS(tyStatic, cl.c, sons = @[n.typ])
  529. result.n = n
  530. else:
  531. result = n.typ
  532. of tyInt, tyFloat:
  533. result = skipIntLit(t, cl.c.idgen)
  534. of tyTypeDesc:
  535. let lookup = cl.typeMap.lookup(t)
  536. if lookup != nil:
  537. result = lookup
  538. if result.kind != tyTypeDesc:
  539. result = makeTypeDesc(cl.c, result)
  540. elif tfUnresolved in t.flags or cl.skipTypedesc:
  541. result = result.base
  542. elif t[0].kind != tyNone:
  543. result = makeTypeDesc(cl.c, replaceTypeVarsT(cl, t[0]))
  544. of tyUserTypeClass, tyStatic:
  545. result = t
  546. of tyGenericInst, tyUserTypeClassInst:
  547. bailout()
  548. result = instCopyType(cl, t)
  549. idTablePut(cl.localCache, t, result)
  550. for i in 1..<result.len:
  551. result[i] = replaceTypeVarsT(cl, result[i])
  552. propagateToOwner(result, result.lastSon)
  553. else:
  554. if containsGenericType(t):
  555. #if not cl.allowMetaTypes:
  556. bailout()
  557. result = instCopyType(cl, t)
  558. result.size = -1 # needs to be recomputed
  559. #if not cl.allowMetaTypes:
  560. idTablePut(cl.localCache, t, result)
  561. for i in 0..<result.len:
  562. if result[i] != nil:
  563. if result[i].kind == tyGenericBody:
  564. localError(cl.c.config, if t.sym != nil: t.sym.info else: cl.info,
  565. "cannot instantiate '" &
  566. typeToString(result[i], preferDesc) &
  567. "' inside of type definition: '" &
  568. t.owner.name.s & "'; Maybe generic arguments are missing?")
  569. var r = replaceTypeVarsT(cl, result[i])
  570. if result.kind == tyObject:
  571. # carefully coded to not skip the precious tyGenericInst:
  572. let r2 = r.skipTypes({tyAlias, tySink, tyOwned})
  573. if r2.kind in {tyPtr, tyRef}:
  574. r = skipTypes(r2, {tyPtr, tyRef})
  575. result[i] = r
  576. if result.kind != tyArray or i != 0:
  577. propagateToOwner(result, r)
  578. # bug #4677: Do not instantiate effect lists
  579. result.n = replaceTypeVarsN(cl, result.n, ord(result.kind==tyProc))
  580. case result.kind
  581. of tyArray:
  582. let idx = result[0]
  583. internalAssert cl.c.config, idx.kind != tyStatic
  584. of tyObject, tyTuple:
  585. propagateFieldFlags(result, result.n)
  586. if result.kind == tyObject and cl.c.computeRequiresInit(cl.c, result):
  587. result.flags.incl tfRequiresInit
  588. of tyProc:
  589. eraseVoidParams(result)
  590. skipIntLiteralParams(result, cl.c.idgen)
  591. of tyRange:
  592. result[0] = result[0].skipTypes({tyStatic, tyDistinct})
  593. else: discard
  594. else:
  595. # If this type doesn't refer to a generic type we may still want to run it
  596. # trough replaceObjBranches in order to resolve any pending nkRecWhen nodes
  597. result = t
  598. # Slow path, we have some work to do
  599. if t.kind == tyRef and t.len > 0 and t[0].kind == tyObject and t[0].n != nil:
  600. discard replaceObjBranches(cl, t[0].n)
  601. elif result.n != nil and t.kind == tyObject:
  602. # Invalidate the type size as we may alter its structure
  603. result.size = -1
  604. result.n = replaceObjBranches(cl, result.n)
  605. proc initTypeVars*(p: PContext, typeMap: LayeredIdTable, info: TLineInfo;
  606. owner: PSym): TReplTypeVars =
  607. result = TReplTypeVars(symMap: initIdTable(),
  608. localCache: initIdTable(), typeMap: typeMap,
  609. info: info, c: p, owner: owner)
  610. proc replaceTypesInBody*(p: PContext, pt: TIdTable, n: PNode;
  611. owner: PSym, allowMetaTypes = false,
  612. fromStaticExpr = false, expectedType: PType = nil): PNode =
  613. var typeMap = initLayeredTypeMap(pt)
  614. var cl = initTypeVars(p, typeMap, n.info, owner)
  615. cl.allowMetaTypes = allowMetaTypes
  616. pushInfoContext(p.config, n.info)
  617. result = replaceTypeVarsN(cl, n, expectedType = expectedType)
  618. popInfoContext(p.config)
  619. when false:
  620. # deadcode
  621. proc replaceTypesForLambda*(p: PContext, pt: TIdTable, n: PNode;
  622. original, new: PSym): PNode =
  623. var typeMap = initLayeredTypeMap(pt)
  624. var cl = initTypeVars(p, typeMap, n.info, original)
  625. idTablePut(cl.symMap, original, new)
  626. pushInfoContext(p.config, n.info)
  627. result = replaceTypeVarsN(cl, n)
  628. popInfoContext(p.config)
  629. proc recomputeFieldPositions*(t: PType; obj: PNode; currPosition: var int) =
  630. if t != nil and t.len > 0 and t[0] != nil:
  631. let b = skipTypes(t[0], skipPtrs)
  632. recomputeFieldPositions(b, b.n, currPosition)
  633. case obj.kind
  634. of nkRecList:
  635. for i in 0..<obj.len: recomputeFieldPositions(nil, obj[i], currPosition)
  636. of nkRecCase:
  637. recomputeFieldPositions(nil, obj[0], currPosition)
  638. for i in 1..<obj.len:
  639. recomputeFieldPositions(nil, lastSon(obj[i]), currPosition)
  640. of nkSym:
  641. obj.sym.position = currPosition
  642. inc currPosition
  643. else: discard "cannot happen"
  644. proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo,
  645. t: PType): PType =
  646. # Given `t` like Foo[T]
  647. # pt: Table with type mappings: T -> int
  648. # Desired result: Foo[int]
  649. # proc (x: T = 0); T -> int ----> proc (x: int = 0)
  650. var typeMap = initLayeredTypeMap(pt)
  651. var cl = initTypeVars(p, typeMap, info, nil)
  652. pushInfoContext(p.config, info)
  653. result = replaceTypeVarsT(cl, t)
  654. popInfoContext(p.config)
  655. let objType = result.skipTypes(abstractInst)
  656. if objType.kind == tyObject:
  657. var position = 0
  658. recomputeFieldPositions(objType, objType.n, position)
  659. proc prepareMetatypeForSigmatch*(p: PContext, pt: TIdTable, info: TLineInfo,
  660. t: PType): PType =
  661. var typeMap = initLayeredTypeMap(pt)
  662. var cl = initTypeVars(p, typeMap, info, nil)
  663. cl.allowMetaTypes = true
  664. pushInfoContext(p.config, info)
  665. result = replaceTypeVarsT(cl, t)
  666. popInfoContext(p.config)
  667. template generateTypeInstance*(p: PContext, pt: TIdTable, arg: PNode,
  668. t: PType): untyped =
  669. generateTypeInstance(p, pt, arg.info, t)