semmagic.nim 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 include file implements the semantic checking for magics.
  10. # included from sem.nim
  11. proc semAddrArg(c: PContext; n: PNode; isUnsafeAddr = false): PNode =
  12. let x = semExprWithType(c, n)
  13. if x.kind == nkSym:
  14. x.sym.flags.incl(sfAddrTaken)
  15. if isAssignable(c, x, isUnsafeAddr) notin {arLValue, arLocalLValue}:
  16. # Do not suggest the use of unsafeAddr if this expression already is a
  17. # unsafeAddr
  18. if isUnsafeAddr:
  19. localError(c.config, n.info, errExprHasNoAddress)
  20. else:
  21. localError(c.config, n.info, errExprHasNoAddress & "; maybe use 'unsafeAddr'")
  22. result = x
  23. proc semTypeOf(c: PContext; n: PNode): PNode =
  24. var m = BiggestInt 1 # typeOfIter
  25. if n.len == 3:
  26. let mode = semConstExpr(c, n[2])
  27. if mode.kind != nkIntLit:
  28. localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
  29. else:
  30. m = mode.intVal
  31. result = newNodeI(nkTypeOfExpr, n.info)
  32. let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  33. result.add typExpr
  34. result.typ = makeTypeDesc(c, typExpr.typ)
  35. type
  36. SemAsgnMode = enum asgnNormal, noOverloadedSubscript, noOverloadedAsgn
  37. proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode
  38. proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode
  39. proc skipAddr(n: PNode): PNode {.inline.} =
  40. (if n.kind == nkHiddenAddr: n[0] else: n)
  41. proc semArrGet(c: PContext; n: PNode; flags: TExprFlags): PNode =
  42. result = newNodeI(nkBracketExpr, n.info)
  43. for i in 1..<n.len: result.add(n[i])
  44. result = semSubscript(c, result, flags)
  45. if result.isNil:
  46. let x = copyTree(n)
  47. x[0] = newIdentNode(getIdent(c.cache, "[]"), n.info)
  48. bracketNotFoundError(c, x)
  49. #localError(c.config, n.info, "could not resolve: " & $n)
  50. result = n
  51. proc semArrPut(c: PContext; n: PNode; flags: TExprFlags): PNode =
  52. # rewrite `[]=`(a, i, x) back to ``a[i] = x``.
  53. let b = newNodeI(nkBracketExpr, n.info)
  54. b.add(n[1].skipAddr)
  55. for i in 2..<n.len-1: b.add(n[i])
  56. result = newNodeI(nkAsgn, n.info, 2)
  57. result[0] = b
  58. result[1] = n.lastSon
  59. result = semAsgn(c, result, noOverloadedSubscript)
  60. proc semAsgnOpr(c: PContext; n: PNode): PNode =
  61. result = newNodeI(nkAsgn, n.info, 2)
  62. result[0] = n[1]
  63. result[1] = n[2]
  64. result = semAsgn(c, result, noOverloadedAsgn)
  65. proc semIsPartOf(c: PContext, n: PNode, flags: TExprFlags): PNode =
  66. var r = isPartOf(n[1], n[2])
  67. result = newIntNodeT(toInt128(ord(r)), n, c.graph)
  68. proc expectIntLit(c: PContext, n: PNode): int =
  69. let x = c.semConstExpr(c, n)
  70. case x.kind
  71. of nkIntLit..nkInt64Lit: result = int(x.intVal)
  72. else: localError(c.config, n.info, errIntLiteralExpected)
  73. proc semInstantiationInfo(c: PContext, n: PNode): PNode =
  74. result = newNodeIT(nkTupleConstr, n.info, n.typ)
  75. let idx = expectIntLit(c, n[1])
  76. let useFullPaths = expectIntLit(c, n[2])
  77. let info = getInfoContext(c.config, idx)
  78. var filename = newNodeIT(nkStrLit, n.info, getSysType(c.graph, n.info, tyString))
  79. filename.strVal = if useFullPaths != 0: toFullPath(c.config, info) else: toFilename(c.config, info)
  80. var line = newNodeIT(nkIntLit, n.info, getSysType(c.graph, n.info, tyInt))
  81. line.intVal = toLinenumber(info)
  82. var column = newNodeIT(nkIntLit, n.info, getSysType(c.graph, n.info, tyInt))
  83. column.intVal = toColumn(info)
  84. # filename: string, line: int, column: int
  85. result.add(newTree(nkExprColonExpr, n.typ.n[0], filename))
  86. result.add(newTree(nkExprColonExpr, n.typ.n[1], line))
  87. result.add(newTree(nkExprColonExpr, n.typ.n[2], column))
  88. proc toNode(t: PType, i: TLineInfo): PNode =
  89. result = newNodeIT(nkType, i, t)
  90. const
  91. # these are types that use the bracket syntax for instantiation
  92. # they can be subjected to the type traits `genericHead` and
  93. # `Uninstantiated`
  94. tyUserDefinedGenerics* = {tyGenericInst, tyGenericInvocation,
  95. tyUserTypeClassInst}
  96. tyMagicGenerics* = {tySet, tySequence, tyArray, tyOpenArray}
  97. tyGenericLike* = tyUserDefinedGenerics +
  98. tyMagicGenerics +
  99. {tyCompositeTypeClass}
  100. proc uninstantiate(t: PType): PType =
  101. result = case t.kind
  102. of tyMagicGenerics: t
  103. of tyUserDefinedGenerics: t.base
  104. of tyCompositeTypeClass: uninstantiate t[1]
  105. else: t
  106. proc getTypeDescNode(typ: PType, sym: PSym, info: TLineInfo): PNode =
  107. var resType = newType(tyTypeDesc, sym)
  108. rawAddSon(resType, typ)
  109. result = toNode(resType, info)
  110. proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym): PNode =
  111. const skippedTypes = {tyTypeDesc, tyAlias, tySink}
  112. let trait = traitCall[0]
  113. internalAssert c.config, trait.kind == nkSym
  114. var operand = operand.skipTypes(skippedTypes)
  115. template operand2: PType =
  116. traitCall[2].typ.skipTypes({tyTypeDesc})
  117. template typeWithSonsResult(kind, sons): PNode =
  118. newTypeWithSons(context, kind, sons).toNode(traitCall.info)
  119. if operand.kind == tyGenericParam or (traitCall.len > 2 and operand2.kind == tyGenericParam):
  120. return traitCall ## too early to evaluate
  121. let s = trait.sym.name.s
  122. case s
  123. of "or", "|":
  124. return typeWithSonsResult(tyOr, @[operand, operand2])
  125. of "and":
  126. return typeWithSonsResult(tyAnd, @[operand, operand2])
  127. of "not":
  128. return typeWithSonsResult(tyNot, @[operand])
  129. of "typeToString":
  130. var prefer = preferTypeName
  131. if traitCall.len >= 2:
  132. let preferStr = traitCall[2].strVal
  133. prefer = parseEnum[TPreferedDesc](preferStr)
  134. result = newStrNode(nkStrLit, operand.typeToString(prefer))
  135. result.typ = getSysType(c.graph, traitCall[1].info, tyString)
  136. result.info = traitCall.info
  137. of "name", "$":
  138. result = newStrNode(nkStrLit, operand.typeToString(preferTypeName))
  139. result.typ = getSysType(c.graph, traitCall[1].info, tyString)
  140. result.info = traitCall.info
  141. of "arity":
  142. result = newIntNode(nkIntLit, operand.len - ord(operand.kind==tyProc))
  143. result.typ = newType(tyInt, context)
  144. result.info = traitCall.info
  145. of "genericHead":
  146. var arg = operand
  147. case arg.kind
  148. of tyGenericInst:
  149. result = getTypeDescNode(arg.base, operand.owner, traitCall.info)
  150. # of tySequence: # this doesn't work
  151. # var resType = newType(tySequence, operand.owner)
  152. # result = toNode(resType, traitCall.info) # doesn't work yet
  153. else:
  154. localError(c.config, traitCall.info, "expected generic type, got: type $2 of kind $1" % [arg.kind.toHumanStr, typeToString(operand)])
  155. result = newType(tyError, context).toNode(traitCall.info)
  156. of "stripGenericParams":
  157. result = uninstantiate(operand).toNode(traitCall.info)
  158. of "supportsCopyMem":
  159. let t = operand.skipTypes({tyVar, tyLent, tyGenericInst, tyAlias, tySink, tyInferred})
  160. let complexObj = containsGarbageCollectedRef(t) or
  161. hasDestructor(t)
  162. result = newIntNodeT(toInt128(ord(not complexObj)), traitCall, c.graph)
  163. of "isNamedTuple":
  164. var operand = operand.skipTypes({tyGenericInst})
  165. let cond = operand.kind == tyTuple and operand.n != nil
  166. result = newIntNodeT(toInt128(ord(cond)), traitCall, c.graph)
  167. of "tupleLen":
  168. var operand = operand.skipTypes({tyGenericInst})
  169. assert operand.kind == tyTuple, $operand.kind
  170. result = newIntNodeT(toInt128(operand.len), traitCall, c.graph)
  171. of "distinctBase":
  172. var arg = operand.skipTypes({tyGenericInst})
  173. if arg.kind == tyDistinct:
  174. while arg.kind == tyDistinct:
  175. arg = arg.base
  176. arg = arg.skipTypes(skippedTypes + {tyGenericInst})
  177. result = getTypeDescNode(arg, operand.owner, traitCall.info)
  178. else:
  179. localError(c.config, traitCall.info,
  180. "distinctBase expects a distinct type as argument. The given type was " & typeToString(operand))
  181. result = newType(tyError, context).toNode(traitCall.info)
  182. else:
  183. localError(c.config, traitCall.info, "unknown trait: " & s)
  184. result = newNodeI(nkEmpty, traitCall.info)
  185. proc semTypeTraits(c: PContext, n: PNode): PNode =
  186. checkMinSonsLen(n, 2, c.config)
  187. let t = n[1].typ
  188. internalAssert c.config, t != nil and t.kind == tyTypeDesc
  189. if t.len > 0:
  190. # This is either a type known to sem or a typedesc
  191. # param to a regular proc (again, known at instantiation)
  192. result = evalTypeTrait(c, n, t, getCurrOwner(c))
  193. else:
  194. # a typedesc variable, pass unmodified to evals
  195. result = n
  196. proc semOrd(c: PContext, n: PNode): PNode =
  197. result = n
  198. let parType = n[1].typ
  199. if isOrdinalType(parType, allowEnumWithHoles=true):
  200. discard
  201. elif parType.kind == tySet:
  202. let a = toInt64(firstOrd(c.config, parType))
  203. let b = toInt64(lastOrd(c.config, parType))
  204. result.typ = makeRangeType(c, a, b, n.info)
  205. else:
  206. localError(c.config, n.info, errOrdinalTypeExpected)
  207. result.typ = errorType(c)
  208. proc semBindSym(c: PContext, n: PNode): PNode =
  209. result = copyNode(n)
  210. result.add(n[0])
  211. let sl = semConstExpr(c, n[1])
  212. if sl.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}:
  213. localError(c.config, n[1].info, errStringLiteralExpected)
  214. return errorNode(c, n)
  215. let isMixin = semConstExpr(c, n[2])
  216. if isMixin.kind != nkIntLit or isMixin.intVal < 0 or
  217. isMixin.intVal > high(TSymChoiceRule).int:
  218. localError(c.config, n[2].info, errConstExprExpected)
  219. return errorNode(c, n)
  220. let id = newIdentNode(getIdent(c.cache, sl.strVal), n.info)
  221. let s = qualifiedLookUp(c, id, {checkUndeclared})
  222. if s != nil:
  223. # we need to mark all symbols:
  224. var sc = symChoice(c, id, s, TSymChoiceRule(isMixin.intVal))
  225. if not (c.inStaticContext > 0 or getCurrOwner(c).isCompileTimeProc):
  226. # inside regular code, bindSym resolves to the sym-choice
  227. # nodes (see tinspectsymbol)
  228. return sc
  229. result.add(sc)
  230. else:
  231. errorUndeclaredIdentifier(c, n[1].info, sl.strVal)
  232. proc opBindSym(c: PContext, scope: PScope, n: PNode, isMixin: int, info: PNode): PNode =
  233. if n.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit, nkIdent}:
  234. localError(c.config, info.info, errStringOrIdentNodeExpected)
  235. return errorNode(c, n)
  236. if isMixin < 0 or isMixin > high(TSymChoiceRule).int:
  237. localError(c.config, info.info, errConstExprExpected)
  238. return errorNode(c, n)
  239. let id = if n.kind == nkIdent: n
  240. else: newIdentNode(getIdent(c.cache, n.strVal), info.info)
  241. let tmpScope = c.currentScope
  242. c.currentScope = scope
  243. let s = qualifiedLookUp(c, id, {checkUndeclared})
  244. if s != nil:
  245. # we need to mark all symbols:
  246. result = symChoice(c, id, s, TSymChoiceRule(isMixin))
  247. else:
  248. errorUndeclaredIdentifier(c, info.info, if n.kind == nkIdent: n.ident.s
  249. else: n.strVal)
  250. c.currentScope = tmpScope
  251. proc semDynamicBindSym(c: PContext, n: PNode): PNode =
  252. # inside regular code, bindSym resolves to the sym-choice
  253. # nodes (see tinspectsymbol)
  254. if not (c.inStaticContext > 0 or getCurrOwner(c).isCompileTimeProc):
  255. return semBindSym(c, n)
  256. if c.graph.vm.isNil:
  257. setupGlobalCtx(c.module, c.graph)
  258. let
  259. vm = PCtx c.graph.vm
  260. # cache the current scope to
  261. # prevent it lost into oblivion
  262. scope = c.currentScope
  263. # cannot use this
  264. # vm.config.features.incl dynamicBindSym
  265. proc bindSymWrapper(a: VmArgs) =
  266. # capture PContext and currentScope
  267. # param description:
  268. # 0. ident, a string literal / computed string / or ident node
  269. # 1. bindSym rule
  270. # 2. info node
  271. a.setResult opBindSym(c, scope, a.getNode(0), a.getInt(1).int, a.getNode(2))
  272. let
  273. # although we use VM callback here, it is not
  274. # executed like 'normal' VM callback
  275. idx = vm.registerCallback("bindSymImpl", bindSymWrapper)
  276. # dummy node to carry idx information to VM
  277. idxNode = newIntTypeNode(idx, c.graph.getSysType(TLineInfo(), tyInt))
  278. result = copyNode(n)
  279. for x in n: result.add x
  280. result.add n # info node
  281. result.add idxNode
  282. proc semShallowCopy(c: PContext, n: PNode, flags: TExprFlags): PNode
  283. proc semOf(c: PContext, n: PNode): PNode =
  284. if n.len == 3:
  285. n[1] = semExprWithType(c, n[1])
  286. n[2] = semExprWithType(c, n[2], {efDetermineType})
  287. #restoreOldStyleType(n[1])
  288. #restoreOldStyleType(n[2])
  289. let a = skipTypes(n[1].typ, abstractPtrs)
  290. let b = skipTypes(n[2].typ, abstractPtrs)
  291. let x = skipTypes(n[1].typ, abstractPtrs-{tyTypeDesc})
  292. let y = skipTypes(n[2].typ, abstractPtrs-{tyTypeDesc})
  293. if x.kind == tyTypeDesc or y.kind != tyTypeDesc:
  294. localError(c.config, n.info, "'of' takes object types")
  295. elif b.kind != tyObject or a.kind != tyObject:
  296. localError(c.config, n.info, "'of' takes object types")
  297. else:
  298. let diff = inheritanceDiff(a, b)
  299. # | returns: 0 iff `a` == `b`
  300. # | returns: -x iff `a` is the x'th direct superclass of `b`
  301. # | returns: +x iff `a` is the x'th direct subclass of `b`
  302. # | returns: `maxint` iff `a` and `b` are not compatible at all
  303. if diff <= 0:
  304. # optimize to true:
  305. message(c.config, n.info, hintConditionAlwaysTrue, renderTree(n))
  306. result = newIntNode(nkIntLit, 1)
  307. result.info = n.info
  308. result.typ = getSysType(c.graph, n.info, tyBool)
  309. return result
  310. elif diff == high(int):
  311. if commonSuperclass(a, b) == nil:
  312. localError(c.config, n.info, "'$1' cannot be of this subtype" % typeToString(a))
  313. else:
  314. message(c.config, n.info, hintConditionAlwaysFalse, renderTree(n))
  315. result = newIntNode(nkIntLit, 0)
  316. result.info = n.info
  317. result.typ = getSysType(c.graph, n.info, tyBool)
  318. else:
  319. localError(c.config, n.info, "'of' takes 2 arguments")
  320. n.typ = getSysType(c.graph, n.info, tyBool)
  321. result = n
  322. proc semUnown(c: PContext; n: PNode): PNode =
  323. proc unownedType(c: PContext; t: PType): PType =
  324. case t.kind
  325. of tyTuple:
  326. var elems = newSeq[PType](t.len)
  327. var someChange = false
  328. for i in 0..<t.len:
  329. elems[i] = unownedType(c, t[i])
  330. if elems[i] != t[i]: someChange = true
  331. if someChange:
  332. result = newType(tyTuple, t.owner)
  333. # we have to use 'rawAddSon' here so that type flags are
  334. # properly computed:
  335. for e in elems: result.rawAddSon(e)
  336. else:
  337. result = t
  338. of tyOwned: result = t[0]
  339. of tySequence, tyOpenArray, tyArray, tyVarargs, tyVar, tyLent,
  340. tyGenericInst, tyAlias:
  341. let b = unownedType(c, t[^1])
  342. if b != t[^1]:
  343. result = copyType(t, t.owner, keepId = false)
  344. result[^1] = b
  345. result.flags.excl tfHasOwned
  346. else:
  347. result = t
  348. else:
  349. result = t
  350. result = copyTree(n[1])
  351. result.typ = unownedType(c, result.typ)
  352. # little hack for injectdestructors.nim (see bug #11350):
  353. #result[0].typ = nil
  354. proc turnFinalizerIntoDestructor(c: PContext; orig: PSym; info: TLineInfo): PSym =
  355. # We need to do 2 things: Replace n.typ which is a 'ref T' by a 'var T' type.
  356. # Replace nkDerefExpr by nkHiddenDeref
  357. # nkDeref is for 'ref T': x[].field
  358. # nkHiddenDeref is for 'var T': x<hidden deref [] here>.field
  359. proc transform(n: PNode; old, fresh: PType; oldParam, newParam: PSym): PNode =
  360. result = shallowCopy(n)
  361. if sameTypeOrNil(n.typ, old):
  362. result.typ = fresh
  363. if n.kind == nkSym and n.sym == oldParam:
  364. result.sym = newParam
  365. for i in 0 ..< safeLen(n):
  366. result[i] = transform(n[i], old, fresh, oldParam, newParam)
  367. #if n.kind == nkDerefExpr and sameType(n[0].typ, old):
  368. # result =
  369. result = copySym(orig)
  370. result.info = info
  371. result.flags.incl sfFromGeneric
  372. result.owner = orig
  373. let origParamType = orig.typ[1]
  374. let newParamType = makeVarType(result, origParamType.skipTypes(abstractPtrs))
  375. let oldParam = orig.typ.n[1].sym
  376. let newParam = newSym(skParam, oldParam.name, result, result.info)
  377. newParam.typ = newParamType
  378. # proc body:
  379. result.ast = transform(orig.ast, origParamType, newParamType, oldParam, newParam)
  380. # proc signature:
  381. result.typ = newProcType(result.info, result)
  382. result.typ.addParam newParam
  383. proc semQuantifier(c: PContext; n: PNode): PNode =
  384. checkMinSonsLen(n, 2, c.config)
  385. openScope(c)
  386. for i in 0..n.len-2:
  387. let v = newSymS(skForVar, n[i], c)
  388. styleCheckDef(c.config, v)
  389. onDef(n.info, v)
  390. n[i] = newSymNode(v)
  391. addDecl(c, v)
  392. n[^1] = forceBool(c, semExprWithType(c, n[^1]))
  393. closeScope(c)
  394. proc semOld(c: PContext; n: PNode): PNode =
  395. if n[1].kind == nkHiddenDeref:
  396. n[1] = n[1][0]
  397. if n[1].kind != nkSym or n[1].sym.kind != skParam:
  398. localError(c.config, n[1].info, "'old' takes a parameter name")
  399. elif n[1].sym.owner != getCurrOwner(c):
  400. localError(c.config, n[1].info, n[1].sym.name.s & " does not belong to " & getCurrOwner(c).name.s)
  401. result = n
  402. proc magicsAfterOverloadResolution(c: PContext, n: PNode,
  403. flags: TExprFlags): PNode =
  404. ## This is the preferred code point to implement magics.
  405. ## ``c`` the current module, a symbol table to a very good approximation
  406. ## ``n`` the ast like it would be passed to a real macro
  407. ## ``flags`` Some flags for more contextual information on how the
  408. ## "macro" is calld.
  409. case n[0].sym.magic
  410. of mAddr:
  411. checkSonsLen(n, 2, c.config)
  412. result = n
  413. result[1] = semAddrArg(c, n[1], n[0].sym.name.s == "unsafeAddr")
  414. result.typ = makePtrType(c, result[1].typ)
  415. of mTypeOf:
  416. result = semTypeOf(c, n)
  417. of mSizeOf:
  418. result = foldSizeOf(c.config, n, n)
  419. of mAlignOf:
  420. result = foldAlignOf(c.config, n, n)
  421. of mOffsetOf:
  422. result = foldOffsetOf(c.config, n, n)
  423. of mArrGet:
  424. result = semArrGet(c, n, flags)
  425. of mArrPut:
  426. result = semArrPut(c, n, flags)
  427. of mAsgn:
  428. if n[0].sym.name.s == "=":
  429. result = semAsgnOpr(c, n)
  430. else:
  431. result = semShallowCopy(c, n, flags)
  432. of mIsPartOf: result = semIsPartOf(c, n, flags)
  433. of mTypeTrait: result = semTypeTraits(c, n)
  434. of mAstToStr:
  435. result = newStrNodeT(renderTree(n[1], {renderNoComments}), n, c.graph)
  436. result.typ = getSysType(c.graph, n.info, tyString)
  437. of mInstantiationInfo: result = semInstantiationInfo(c, n)
  438. of mOrd: result = semOrd(c, n)
  439. of mOf: result = semOf(c, n)
  440. of mHigh, mLow: result = semLowHigh(c, n, n[0].sym.magic)
  441. of mShallowCopy: result = semShallowCopy(c, n, flags)
  442. of mNBindSym:
  443. if dynamicBindSym notin c.features:
  444. result = semBindSym(c, n)
  445. else:
  446. result = semDynamicBindSym(c, n)
  447. of mProcCall:
  448. result = n
  449. result.typ = n[1].typ
  450. of mDotDot:
  451. result = n
  452. of mPlugin:
  453. let plugin = getPlugin(c.cache, n[0].sym)
  454. if plugin.isNil:
  455. localError(c.config, n.info, "cannot find plugin " & n[0].sym.name.s)
  456. result = n
  457. else:
  458. result = plugin(c, n)
  459. of mNewFinalize:
  460. # Make sure the finalizer procedure refers to a procedure
  461. if n[^1].kind == nkSym and n[^1].sym.kind notin {skProc, skFunc}:
  462. localError(c.config, n.info, "finalizer must be a direct reference to a proc")
  463. elif optTinyRtti in c.config.globalOptions:
  464. let fin = if n[^1].kind == nkLambda: n[^1][namePos].sym
  465. else: n[^1].sym
  466. # check if we converted this finalizer into a destructor already:
  467. let t = whereToBindTypeHook(c, fin.typ[1].skipTypes(abstractInst+{tyRef}))
  468. if t != nil and t.attachedOps[attachedDestructor] != nil and t.attachedOps[attachedDestructor].owner == fin:
  469. discard "already turned this one into a finalizer"
  470. else:
  471. bindTypeHook(c, turnFinalizerIntoDestructor(c, fin, n.info), n, attachedDestructor)
  472. result = n
  473. of mDestroy:
  474. result = n
  475. let t = n[1].typ.skipTypes(abstractVar)
  476. if t.destructor != nil:
  477. result[0] = newSymNode(t.destructor)
  478. of mUnown:
  479. result = semUnown(c, n)
  480. of mExists, mForall:
  481. result = semQuantifier(c, n)
  482. of mOld:
  483. result = semOld(c, n)
  484. of mSetLengthSeq:
  485. result = n
  486. let seqType = result[1].typ.skipTypes({tyPtr, tyRef, # in case we had auto-dereferencing
  487. tyVar, tyGenericInst, tyOwned, tySink,
  488. tyAlias, tyUserTypeClassInst})
  489. if seqType.kind == tySequence and seqType.base.requiresInit:
  490. message(c.config, n.info, warnUnsafeSetLen, typeToString(seqType.base))
  491. of mDefault:
  492. result = n
  493. c.config.internalAssert result[1].typ.kind == tyTypeDesc
  494. let constructed = result[1].typ.base
  495. if constructed.requiresInit:
  496. message(c.config, n.info, warnUnsafeDefault, typeToString(constructed))
  497. of mPred:
  498. if n[1].typ.skipTypes(abstractInst).kind in {tyUInt..tyUInt64}:
  499. n[0].sym.magic = mSubU
  500. result = n
  501. else:
  502. result = n