semtypinst.nim 25 KB

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