semmagic.nim 21 KB

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