semmagic.nim 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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.idgen, 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, nextTypeId 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, nextTypeId 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, nextTypeId 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.idgen, 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.idgen, 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.idgen, c.graph)
  169. of "distinctBase":
  170. var arg = operand.skipTypes({tyGenericInst})
  171. let rec = semConstExpr(c, traitCall[2]).intVal != 0
  172. while arg.kind == tyDistinct:
  173. arg = arg.base.skipTypes(skippedTypes + {tyGenericInst})
  174. if not rec: break
  175. result = getTypeDescNode(c, arg, operand.owner, traitCall.info)
  176. else:
  177. localError(c.config, traitCall.info, "unknown trait: " & s)
  178. result = newNodeI(nkEmpty, traitCall.info)
  179. proc semTypeTraits(c: PContext, n: PNode): PNode =
  180. checkMinSonsLen(n, 2, c.config)
  181. let t = n[1].typ
  182. internalAssert c.config, t != nil and t.kind == tyTypeDesc
  183. if t.len > 0:
  184. # This is either a type known to sem or a typedesc
  185. # param to a regular proc (again, known at instantiation)
  186. result = evalTypeTrait(c, n, t, getCurrOwner(c))
  187. else:
  188. # a typedesc variable, pass unmodified to evals
  189. result = n
  190. proc semOrd(c: PContext, n: PNode): PNode =
  191. result = n
  192. let parType = n[1].typ
  193. if isOrdinalType(parType, allowEnumWithHoles=true):
  194. discard
  195. else:
  196. localError(c.config, n.info, errOrdinalTypeExpected)
  197. result.typ = errorType(c)
  198. proc semBindSym(c: PContext, n: PNode): PNode =
  199. result = copyNode(n)
  200. result.add(n[0])
  201. let sl = semConstExpr(c, n[1])
  202. if sl.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}:
  203. return localErrorNode(c, n, n[1].info, errStringLiteralExpected)
  204. let isMixin = semConstExpr(c, n[2])
  205. if isMixin.kind != nkIntLit or isMixin.intVal < 0 or
  206. isMixin.intVal > high(TSymChoiceRule).int:
  207. return localErrorNode(c, n, n[2].info, errConstExprExpected)
  208. let id = newIdentNode(getIdent(c.cache, sl.strVal), n.info)
  209. let s = qualifiedLookUp(c, id, {checkUndeclared})
  210. if s != nil:
  211. # we need to mark all symbols:
  212. var sc = symChoice(c, id, s, TSymChoiceRule(isMixin.intVal))
  213. if not (c.inStaticContext > 0 or getCurrOwner(c).isCompileTimeProc):
  214. # inside regular code, bindSym resolves to the sym-choice
  215. # nodes (see tinspectsymbol)
  216. return sc
  217. result.add(sc)
  218. else:
  219. errorUndeclaredIdentifier(c, n[1].info, sl.strVal)
  220. proc opBindSym(c: PContext, scope: PScope, n: PNode, isMixin: int, info: PNode): PNode =
  221. if n.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit, nkIdent}:
  222. return localErrorNode(c, n, info.info, errStringOrIdentNodeExpected)
  223. if isMixin < 0 or isMixin > high(TSymChoiceRule).int:
  224. return localErrorNode(c, n, info.info, errConstExprExpected)
  225. let id = if n.kind == nkIdent: n
  226. else: newIdentNode(getIdent(c.cache, n.strVal), info.info)
  227. let tmpScope = c.currentScope
  228. c.currentScope = scope
  229. let s = qualifiedLookUp(c, id, {checkUndeclared})
  230. if s != nil:
  231. # we need to mark all symbols:
  232. result = symChoice(c, id, s, TSymChoiceRule(isMixin))
  233. else:
  234. errorUndeclaredIdentifier(c, info.info, if n.kind == nkIdent: n.ident.s
  235. else: n.strVal)
  236. c.currentScope = tmpScope
  237. proc semDynamicBindSym(c: PContext, n: PNode): PNode =
  238. # inside regular code, bindSym resolves to the sym-choice
  239. # nodes (see tinspectsymbol)
  240. if not (c.inStaticContext > 0 or getCurrOwner(c).isCompileTimeProc):
  241. return semBindSym(c, n)
  242. if c.graph.vm.isNil:
  243. setupGlobalCtx(c.module, c.graph, c.idgen)
  244. let
  245. vm = PCtx c.graph.vm
  246. # cache the current scope to
  247. # prevent it lost into oblivion
  248. scope = c.currentScope
  249. # cannot use this
  250. # vm.config.features.incl dynamicBindSym
  251. proc bindSymWrapper(a: VmArgs) =
  252. # capture PContext and currentScope
  253. # param description:
  254. # 0. ident, a string literal / computed string / or ident node
  255. # 1. bindSym rule
  256. # 2. info node
  257. a.setResult opBindSym(c, scope, a.getNode(0), a.getInt(1).int, a.getNode(2))
  258. let
  259. # although we use VM callback here, it is not
  260. # executed like 'normal' VM callback
  261. idx = vm.registerCallback("bindSymImpl", bindSymWrapper)
  262. # dummy node to carry idx information to VM
  263. idxNode = newIntTypeNode(idx, c.graph.getSysType(TLineInfo(), tyInt))
  264. result = copyNode(n)
  265. for x in n: result.add x
  266. result.add n # info node
  267. result.add idxNode
  268. proc semShallowCopy(c: PContext, n: PNode, flags: TExprFlags): PNode
  269. proc semOf(c: PContext, n: PNode): PNode =
  270. if n.len == 3:
  271. n[1] = semExprWithType(c, n[1])
  272. n[2] = semExprWithType(c, n[2], {efDetermineType})
  273. #restoreOldStyleType(n[1])
  274. #restoreOldStyleType(n[2])
  275. let a = skipTypes(n[1].typ, abstractPtrs)
  276. let b = skipTypes(n[2].typ, abstractPtrs)
  277. let x = skipTypes(n[1].typ, abstractPtrs-{tyTypeDesc})
  278. let y = skipTypes(n[2].typ, abstractPtrs-{tyTypeDesc})
  279. if x.kind == tyTypeDesc or y.kind != tyTypeDesc:
  280. localError(c.config, n.info, "'of' takes object types")
  281. elif b.kind != tyObject or a.kind != tyObject:
  282. localError(c.config, n.info, "'of' takes object types")
  283. else:
  284. let diff = inheritanceDiff(a, b)
  285. # | returns: 0 iff `a` == `b`
  286. # | returns: -x iff `a` is the x'th direct superclass of `b`
  287. # | returns: +x iff `a` is the x'th direct subclass of `b`
  288. # | returns: `maxint` iff `a` and `b` are not compatible at all
  289. if diff <= 0:
  290. # optimize to true:
  291. message(c.config, n.info, hintConditionAlwaysTrue, renderTree(n))
  292. result = newIntNode(nkIntLit, 1)
  293. result.info = n.info
  294. result.typ = getSysType(c.graph, n.info, tyBool)
  295. return result
  296. elif diff == high(int):
  297. if commonSuperclass(a, b) == nil:
  298. localError(c.config, n.info, "'$1' cannot be of this subtype" % typeToString(a))
  299. else:
  300. message(c.config, n.info, hintConditionAlwaysFalse, renderTree(n))
  301. result = newIntNode(nkIntLit, 0)
  302. result.info = n.info
  303. result.typ = getSysType(c.graph, n.info, tyBool)
  304. else:
  305. localError(c.config, n.info, "'of' takes 2 arguments")
  306. n.typ = getSysType(c.graph, n.info, tyBool)
  307. result = n
  308. proc semUnown(c: PContext; n: PNode): PNode =
  309. proc unownedType(c: PContext; t: PType): PType =
  310. case t.kind
  311. of tyTuple:
  312. var elems = newSeq[PType](t.len)
  313. var someChange = false
  314. for i in 0..<t.len:
  315. elems[i] = unownedType(c, t[i])
  316. if elems[i] != t[i]: someChange = true
  317. if someChange:
  318. result = newType(tyTuple, nextTypeId c.idgen, t.owner)
  319. # we have to use 'rawAddSon' here so that type flags are
  320. # properly computed:
  321. for e in elems: result.rawAddSon(e)
  322. else:
  323. result = t
  324. of tyOwned: result = t[0]
  325. of tySequence, tyOpenArray, tyArray, tyVarargs, tyVar, tyLent,
  326. tyGenericInst, tyAlias:
  327. let b = unownedType(c, t[^1])
  328. if b != t[^1]:
  329. result = copyType(t, nextTypeId c.idgen, t.owner)
  330. copyTypeProps(c.graph, c.idgen.module, result, t)
  331. result[^1] = b
  332. result.flags.excl tfHasOwned
  333. else:
  334. result = t
  335. else:
  336. result = t
  337. result = copyTree(n[1])
  338. result.typ = unownedType(c, result.typ)
  339. # little hack for injectdestructors.nim (see bug #11350):
  340. #result[0].typ = nil
  341. proc turnFinalizerIntoDestructor(c: PContext; orig: PSym; info: TLineInfo): PSym =
  342. # We need to do 2 things: Replace n.typ which is a 'ref T' by a 'var T' type.
  343. # Replace nkDerefExpr by nkHiddenDeref
  344. # nkDeref is for 'ref T': x[].field
  345. # nkHiddenDeref is for 'var T': x<hidden deref [] here>.field
  346. proc transform(c: PContext; procSym: PSym; n: PNode; old, fresh: PType; oldParam, newParam: PSym): PNode =
  347. result = shallowCopy(n)
  348. if sameTypeOrNil(n.typ, old):
  349. result.typ = fresh
  350. if n.kind == nkSym:
  351. if n.sym == oldParam:
  352. result.sym = newParam
  353. elif n.sym.owner == orig:
  354. result.sym = copySym(n.sym, nextSymId c.idgen)
  355. result.sym.owner = procSym
  356. for i in 0 ..< safeLen(n):
  357. result[i] = transform(c, procSym, n[i], old, fresh, oldParam, newParam)
  358. #if n.kind == nkDerefExpr and sameType(n[0].typ, old):
  359. # result =
  360. result = copySym(orig, nextSymId c.idgen)
  361. result.info = info
  362. result.flags.incl sfFromGeneric
  363. result.owner = orig
  364. let origParamType = orig.typ[1]
  365. let newParamType = makeVarType(result, origParamType.skipTypes(abstractPtrs), c.idgen)
  366. let oldParam = orig.typ.n[1].sym
  367. let newParam = newSym(skParam, oldParam.name, nextSymId c.idgen, result, result.info)
  368. newParam.typ = newParamType
  369. # proc body:
  370. result.ast = transform(c, result, orig.ast, origParamType, newParamType, oldParam, newParam)
  371. # proc signature:
  372. result.typ = newProcType(result.info, nextTypeId c.idgen, result)
  373. result.typ.addParam newParam
  374. proc semQuantifier(c: PContext; n: PNode): PNode =
  375. checkSonsLen(n, 2, c.config)
  376. openScope(c)
  377. result = newNodeIT(n.kind, n.info, n.typ)
  378. result.add n[0]
  379. let args = n[1]
  380. assert args.kind == nkArgList
  381. for i in 0..args.len-2:
  382. let it = args[i]
  383. var valid = false
  384. if it.kind == nkInfix:
  385. let op = considerQuotedIdent(c, it[0])
  386. if op.id == ord(wIn):
  387. let v = newSymS(skForVar, it[1], c)
  388. styleCheckDef(c, v)
  389. onDef(it[1].info, v)
  390. let domain = semExprWithType(c, it[2], {efWantIterator})
  391. v.typ = domain.typ
  392. valid = true
  393. addDecl(c, v)
  394. result.add newTree(nkInfix, it[0], newSymNode(v), domain)
  395. if not valid:
  396. localError(c.config, n.info, "<quantifier> 'in' <range> expected")
  397. result.add forceBool(c, semExprWithType(c, args[^1]))
  398. closeScope(c)
  399. proc semOld(c: PContext; n: PNode): PNode =
  400. if n[1].kind == nkHiddenDeref:
  401. n[1] = n[1][0]
  402. if n[1].kind != nkSym or n[1].sym.kind != skParam:
  403. localError(c.config, n[1].info, "'old' takes a parameter name")
  404. elif n[1].sym.owner != getCurrOwner(c):
  405. localError(c.config, n[1].info, n[1].sym.name.s & " does not belong to " & getCurrOwner(c).name.s)
  406. result = n
  407. proc semPrivateAccess(c: PContext, n: PNode): PNode =
  408. let t = n[1].typ[0].toObjectFromRefPtrGeneric
  409. c.currentScope.allowPrivateAccess.add t.sym
  410. result = newNodeIT(nkEmpty, n.info, getSysType(c.graph, n.info, tyVoid))
  411. proc magicsAfterOverloadResolution(c: PContext, n: PNode,
  412. flags: TExprFlags): PNode =
  413. ## This is the preferred code point to implement magics.
  414. ## ``c`` the current module, a symbol table to a very good approximation
  415. ## ``n`` the ast like it would be passed to a real macro
  416. ## ``flags`` Some flags for more contextual information on how the
  417. ## "macro" is calld.
  418. case n[0].sym.magic
  419. of mAddr:
  420. checkSonsLen(n, 2, c.config)
  421. result = n
  422. result[1] = semAddrArg(c, n[1], n[0].sym.name.s == "unsafeAddr")
  423. result.typ = makePtrType(c, result[1].typ)
  424. of mTypeOf:
  425. result = semTypeOf(c, n)
  426. of mSizeOf:
  427. result = foldSizeOf(c.config, n, n)
  428. of mAlignOf:
  429. result = foldAlignOf(c.config, n, n)
  430. of mOffsetOf:
  431. result = foldOffsetOf(c.config, n, n)
  432. of mArrGet:
  433. result = semArrGet(c, n, flags)
  434. of mArrPut:
  435. result = semArrPut(c, n, flags)
  436. of mAsgn:
  437. if n[0].sym.name.s == "=":
  438. result = semAsgnOpr(c, n)
  439. else:
  440. result = semShallowCopy(c, n, flags)
  441. of mIsPartOf: result = semIsPartOf(c, n, flags)
  442. of mTypeTrait: result = semTypeTraits(c, n)
  443. of mAstToStr:
  444. result = newStrNodeT(renderTree(n[1], {renderNoComments}), n, c.graph)
  445. result.typ = getSysType(c.graph, n.info, tyString)
  446. of mInstantiationInfo: result = semInstantiationInfo(c, n)
  447. of mOrd: result = semOrd(c, n)
  448. of mOf: result = semOf(c, n)
  449. of mHigh, mLow: result = semLowHigh(c, n, n[0].sym.magic)
  450. of mShallowCopy: result = semShallowCopy(c, n, flags)
  451. of mNBindSym:
  452. if dynamicBindSym notin c.features:
  453. result = semBindSym(c, n)
  454. else:
  455. result = semDynamicBindSym(c, n)
  456. of mProcCall:
  457. result = n
  458. result.typ = n[1].typ
  459. of mDotDot:
  460. result = n
  461. of mPlugin:
  462. let plugin = getPlugin(c.cache, n[0].sym)
  463. if plugin.isNil:
  464. localError(c.config, n.info, "cannot find plugin " & n[0].sym.name.s)
  465. result = n
  466. else:
  467. result = plugin(c, n)
  468. of mNewFinalize:
  469. # Make sure the finalizer procedure refers to a procedure
  470. if n[^1].kind == nkSym and n[^1].sym.kind notin {skProc, skFunc}:
  471. localError(c.config, n.info, "finalizer must be a direct reference to a proc")
  472. elif optTinyRtti in c.config.globalOptions:
  473. let nfin = skipConvCastAndClosure(n[^1])
  474. let fin = case nfin.kind
  475. of nkSym: nfin.sym
  476. of nkLambda, nkDo: nfin[namePos].sym
  477. else:
  478. localError(c.config, n.info, "finalizer must be a direct reference to a proc")
  479. nil
  480. if fin != nil:
  481. if fin.kind notin {skProc, skFunc}:
  482. # calling convention is checked in codegen
  483. localError(c.config, n.info, "finalizer must be a direct reference to a proc")
  484. # check if we converted this finalizer into a destructor already:
  485. let t = whereToBindTypeHook(c, fin.typ[1].skipTypes(abstractInst+{tyRef}))
  486. if t != nil and getAttachedOp(c.graph, t, attachedDestructor) != nil and
  487. getAttachedOp(c.graph, t, attachedDestructor).owner == fin:
  488. discard "already turned this one into a finalizer"
  489. else:
  490. bindTypeHook(c, turnFinalizerIntoDestructor(c, fin, n.info), n, attachedDestructor)
  491. result = n
  492. of mDestroy:
  493. result = n
  494. let t = n[1].typ.skipTypes(abstractVar)
  495. let op = getAttachedOp(c.graph, t, attachedDestructor)
  496. if op != nil:
  497. result[0] = newSymNode(op)
  498. of mTrace:
  499. result = n
  500. let t = n[1].typ.skipTypes(abstractVar)
  501. let op = getAttachedOp(c.graph, t, attachedTrace)
  502. if op != nil:
  503. result[0] = newSymNode(op)
  504. of mUnown:
  505. result = semUnown(c, n)
  506. of mExists, mForall:
  507. result = semQuantifier(c, n)
  508. of mOld:
  509. result = semOld(c, n)
  510. of mSetLengthSeq:
  511. result = n
  512. let seqType = result[1].typ.skipTypes({tyPtr, tyRef, # in case we had auto-dereferencing
  513. tyVar, tyGenericInst, tyOwned, tySink,
  514. tyAlias, tyUserTypeClassInst})
  515. if seqType.kind == tySequence and seqType.base.requiresInit:
  516. message(c.config, n.info, warnUnsafeSetLen, typeToString(seqType.base))
  517. of mDefault:
  518. result = n
  519. c.config.internalAssert result[1].typ.kind == tyTypeDesc
  520. let constructed = result[1].typ.base
  521. if constructed.requiresInit:
  522. message(c.config, n.info, warnUnsafeDefault, typeToString(constructed))
  523. of mIsolate:
  524. if not checkIsolate(n[1]):
  525. localError(c.config, n.info, "expression cannot be isolated: " & $n[1])
  526. result = n
  527. of mPred:
  528. if n[1].typ.skipTypes(abstractInst).kind in {tyUInt..tyUInt64}:
  529. n[0].sym.magic = mSubU
  530. result = n
  531. of mPrivateAccess:
  532. result = semPrivateAccess(c, n)
  533. else:
  534. result = n