semmagic.nim 22 KB

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