semtypinst.nim 31 KB

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