semtempl.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. # included from sem.nim
  10. discard """
  11. hygienic templates:
  12. template `||` (a, b: untyped): untyped =
  13. let aa = a
  14. if aa: aa else: b
  15. var
  16. a, b: T
  17. echo a || b || a
  18. Each evaluation context has to be different and we need to perform
  19. some form of preliminary symbol lookup in template definitions. Hygiene is
  20. a way to achieve lexical scoping at compile time.
  21. """
  22. const
  23. errImplOfXNotAllowed = "implementation of '$1' is not allowed"
  24. type
  25. TSymBinding = enum
  26. spNone, spGenSym, spInject
  27. proc symBinding(n: PNode): TSymBinding =
  28. for i in 0..<n.len:
  29. var it = n[i]
  30. var key = if it.kind == nkExprColonExpr: it[0] else: it
  31. if key.kind == nkIdent:
  32. case whichKeyword(key.ident)
  33. of wGensym: return spGenSym
  34. of wInject: return spInject
  35. else: discard
  36. type
  37. TSymChoiceRule = enum
  38. scClosed, scOpen, scForceOpen
  39. proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule;
  40. isField = false): PNode =
  41. var
  42. a: PSym
  43. o: TOverloadIter
  44. var i = 0
  45. a = initOverloadIter(o, c, n)
  46. while a != nil:
  47. if a.kind != skModule:
  48. inc(i)
  49. if i > 1: break
  50. a = nextOverloadIter(o, c, n)
  51. let info = getCallLineInfo(n)
  52. if i <= 1 and r != scForceOpen:
  53. # XXX this makes more sense but breaks bootstrapping for now:
  54. # (s.kind notin routineKinds or s.magic != mNone):
  55. # for instance 'nextTry' is both in tables.nim and astalgo.nim ...
  56. if not isField or sfGenSym notin s.flags:
  57. result = newSymNode(s, info)
  58. markUsed(c, info, s)
  59. onUse(info, s)
  60. else:
  61. result = n
  62. elif i == 0:
  63. # forced open but symbol not in scope, retain information
  64. result = n
  65. else:
  66. # semantic checking requires a type; ``fitNode`` deals with it
  67. # appropriately
  68. let kind = if r == scClosed or n.kind == nkDotExpr: nkClosedSymChoice
  69. else: nkOpenSymChoice
  70. result = newNodeIT(kind, info, newTypeS(tyNone, c))
  71. a = initOverloadIter(o, c, n)
  72. while a != nil:
  73. if a.kind != skModule and (not isField or sfGenSym notin a.flags):
  74. incl(a.flags, sfUsed)
  75. markOwnerModuleAsUsed(c, a)
  76. result.add newSymNode(a, info)
  77. onUse(info, a)
  78. a = nextOverloadIter(o, c, n)
  79. proc semBindStmt(c: PContext, n: PNode, toBind: var IntSet): PNode =
  80. result = copyNode(n)
  81. for i in 0..<n.len:
  82. var a = n[i]
  83. # If 'a' is an overloaded symbol, we used to use the first symbol
  84. # as a 'witness' and use the fact that subsequent lookups will yield
  85. # the same symbol!
  86. # This is however not true anymore for hygienic templates as semantic
  87. # processing for them changes the symbol table...
  88. let s = qualifiedLookUp(c, a, {checkUndeclared})
  89. if s != nil:
  90. # we need to mark all symbols:
  91. let sc = symChoice(c, n, s, scClosed)
  92. if sc.kind == nkSym:
  93. toBind.incl(sc.sym.id)
  94. result.add sc
  95. else:
  96. for x in items(sc):
  97. toBind.incl(x.sym.id)
  98. result.add x
  99. else:
  100. illFormedAst(a, c.config)
  101. proc semMixinStmt(c: PContext, n: PNode, toMixin: var IntSet): PNode =
  102. result = copyNode(n)
  103. for i in 0..<n.len:
  104. toMixin.incl(considerQuotedIdent(c, n[i]).id)
  105. let x = symChoice(c, n[i], nil, scForceOpen)
  106. result.add x
  107. proc replaceIdentBySym(c: PContext; n: var PNode, s: PNode) =
  108. case n.kind
  109. of nkPostfix: replaceIdentBySym(c, n[1], s)
  110. of nkPragmaExpr: replaceIdentBySym(c, n[0], s)
  111. of nkIdent, nkAccQuoted, nkSym: n = s
  112. else: illFormedAst(n, c.config)
  113. type
  114. TemplCtx = object
  115. c: PContext
  116. toBind, toMixin, toInject: IntSet
  117. owner: PSym
  118. cursorInBody: bool # only for nimsuggest
  119. scopeN: int
  120. noGenSym: int
  121. inTemplateHeader: int
  122. proc getIdentNode(c: var TemplCtx, n: PNode): PNode =
  123. case n.kind
  124. of nkPostfix: result = getIdentNode(c, n[1])
  125. of nkPragmaExpr: result = getIdentNode(c, n[0])
  126. of nkIdent:
  127. result = n
  128. let s = qualifiedLookUp(c.c, n, {})
  129. if s != nil:
  130. if s.owner == c.owner and s.kind == skParam:
  131. result = newSymNode(s, n.info)
  132. of nkAccQuoted, nkSym: result = n
  133. else:
  134. illFormedAst(n, c.c.config)
  135. result = n
  136. proc isTemplParam(c: TemplCtx, n: PNode): bool {.inline.} =
  137. result = n.kind == nkSym and n.sym.kind == skParam and
  138. n.sym.owner == c.owner and sfTemplateParam in n.sym.flags
  139. proc semTemplBody(c: var TemplCtx, n: PNode): PNode
  140. proc openScope(c: var TemplCtx) =
  141. openScope(c.c)
  142. proc closeScope(c: var TemplCtx) =
  143. closeScope(c.c)
  144. proc semTemplBodyScope(c: var TemplCtx, n: PNode): PNode =
  145. openScope(c)
  146. result = semTemplBody(c, n)
  147. closeScope(c)
  148. proc onlyReplaceParams(c: var TemplCtx, n: PNode): PNode =
  149. result = n
  150. if n.kind == nkIdent:
  151. let s = qualifiedLookUp(c.c, n, {})
  152. if s != nil:
  153. if s.owner == c.owner and s.kind == skParam:
  154. incl(s.flags, sfUsed)
  155. result = newSymNode(s, n.info)
  156. onUse(n.info, s)
  157. else:
  158. for i in 0..<n.safeLen:
  159. result[i] = onlyReplaceParams(c, n[i])
  160. proc newGenSym(kind: TSymKind, n: PNode, c: var TemplCtx): PSym =
  161. result = newSym(kind, considerQuotedIdent(c.c, n), c.c.idgen, c.owner, n.info)
  162. incl(result.flags, sfGenSym)
  163. incl(result.flags, sfShadowed)
  164. proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
  165. # locals default to 'gensym', fields default to 'inject':
  166. if (n.kind == nkPragmaExpr and symBinding(n[1]) == spInject) or
  167. k == skField:
  168. # even if injected, don't produce a sym choice here:
  169. #n = semTemplBody(c, n)
  170. var x = n
  171. while true:
  172. case x.kind
  173. of nkPostfix: x = x[1]
  174. of nkPragmaExpr: x = x[0]
  175. of nkIdent: break
  176. of nkAccQuoted:
  177. # consider: type `T TemplParam` {.inject.}
  178. # it suffices to return to treat it like 'inject':
  179. n = onlyReplaceParams(c, n)
  180. return
  181. else:
  182. illFormedAst(x, c.c.config)
  183. let ident = getIdentNode(c, x)
  184. if not isTemplParam(c, ident):
  185. if k != skField: c.toInject.incl(x.ident.id)
  186. else:
  187. replaceIdentBySym(c.c, n, ident)
  188. else:
  189. if (n.kind == nkPragmaExpr and n.len >= 2 and n[1].kind == nkPragma):
  190. let pragmaNode = n[1]
  191. for i in 0..<pragmaNode.len:
  192. let ni = pragmaNode[i]
  193. # see D20210801T100514
  194. var found = false
  195. if ni.kind == nkIdent:
  196. for a in templatePragmas:
  197. if ni.ident == getIdent(c.c.cache, $a):
  198. found = true
  199. break
  200. if not found:
  201. openScope(c)
  202. pragmaNode[i] = semTemplBody(c, pragmaNode[i])
  203. closeScope(c)
  204. let ident = getIdentNode(c, n)
  205. if not isTemplParam(c, ident):
  206. if n.kind != nkSym and not (n.kind == nkIdent and n.ident.id == ord(wUnderscore)):
  207. let local = newGenSym(k, ident, c)
  208. addPrelimDecl(c.c, local)
  209. styleCheckDef(c.c, n.info, local)
  210. onDef(n.info, local)
  211. replaceIdentBySym(c.c, n, newSymNode(local, n.info))
  212. if k == skParam and c.inTemplateHeader > 0:
  213. local.flags.incl sfTemplateParam
  214. else:
  215. replaceIdentBySym(c.c, n, ident)
  216. proc semTemplSymbol(c: PContext, n: PNode, s: PSym; isField: bool): PNode =
  217. incl(s.flags, sfUsed)
  218. # bug #12885; ideally sem'checking is performed again afterwards marking
  219. # the symbol as used properly, but the nfSem mechanism currently prevents
  220. # that from happening, so we mark the module as used here already:
  221. markOwnerModuleAsUsed(c, s)
  222. # we do not call onUse here, as the identifier is not really
  223. # resolved here. We will fixup the used identifiers later.
  224. case s.kind
  225. of skUnknown:
  226. # Introduced in this pass! Leave it as an identifier.
  227. result = n
  228. of OverloadableSyms-{skTemplate,skMacro}:
  229. result = symChoice(c, n, s, scOpen, isField)
  230. of skTemplate, skMacro:
  231. result = symChoice(c, n, s, scOpen, isField)
  232. if result.kind == nkSym:
  233. # template/macro symbols might need to be semchecked again
  234. # prepareOperand etc don't do this without setting the type to nil
  235. result.typ = nil
  236. of skGenericParam:
  237. if isField and sfGenSym in s.flags: result = n
  238. else: result = newSymNodeTypeDesc(s, c.idgen, n.info)
  239. of skParam:
  240. result = n
  241. of skType:
  242. if isField and sfGenSym in s.flags: result = n
  243. else: result = newSymNodeTypeDesc(s, c.idgen, n.info)
  244. else:
  245. if isField and sfGenSym in s.flags: result = n
  246. else: result = newSymNode(s, n.info)
  247. # Issue #12832
  248. when defined(nimsuggest):
  249. suggestSym(c.graph, n.info, s, c.graph.usageSym, false)
  250. # field access (dot expr) will be handled by builtinFieldAccess
  251. if not isField:
  252. styleCheckUse(c, n.info, s)
  253. proc semRoutineInTemplName(c: var TemplCtx, n: PNode): PNode =
  254. result = n
  255. if n.kind == nkIdent:
  256. let s = qualifiedLookUp(c.c, n, {})
  257. if s != nil:
  258. if s.owner == c.owner and (s.kind == skParam or sfGenSym in s.flags):
  259. incl(s.flags, sfUsed)
  260. result = newSymNode(s, n.info)
  261. onUse(n.info, s)
  262. else:
  263. for i in 0..<n.safeLen:
  264. result[i] = semRoutineInTemplName(c, n[i])
  265. proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
  266. result = n
  267. checkSonsLen(n, bodyPos + 1, c.c.config)
  268. # routines default to 'inject':
  269. if n.kind notin nkLambdaKinds and symBinding(n[pragmasPos]) == spGenSym:
  270. let ident = getIdentNode(c, n[namePos])
  271. if not isTemplParam(c, ident):
  272. var s = newGenSym(k, ident, c)
  273. s.ast = n
  274. addPrelimDecl(c.c, s)
  275. styleCheckDef(c.c, n.info, s)
  276. onDef(n.info, s)
  277. n[namePos] = newSymNode(s, n[namePos].info)
  278. else:
  279. n[namePos] = ident
  280. else:
  281. n[namePos] = semRoutineInTemplName(c, n[namePos])
  282. # open scope for parameters
  283. openScope(c)
  284. for i in patternPos..paramsPos-1:
  285. n[i] = semTemplBody(c, n[i])
  286. if k == skTemplate: inc(c.inTemplateHeader)
  287. n[paramsPos] = semTemplBody(c, n[paramsPos])
  288. if k == skTemplate: dec(c.inTemplateHeader)
  289. for i in paramsPos+1..miscPos:
  290. n[i] = semTemplBody(c, n[i])
  291. # open scope for locals
  292. inc c.scopeN
  293. openScope(c)
  294. n[bodyPos] = semTemplBody(c, n[bodyPos])
  295. # close scope for locals
  296. closeScope(c)
  297. dec c.scopeN
  298. # close scope for parameters
  299. closeScope(c)
  300. proc semTemplIdentDef(c: var TemplCtx, a: PNode, symKind: TSymKind) =
  301. checkMinSonsLen(a, 3, c.c.config)
  302. when defined(nimsuggest):
  303. inc c.c.inTypeContext
  304. a[^2] = semTemplBody(c, a[^2])
  305. when defined(nimsuggest):
  306. dec c.c.inTypeContext
  307. a[^1] = semTemplBody(c, a[^1])
  308. for j in 0..<a.len-2:
  309. addLocalDecl(c, a[j], symKind)
  310. proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start = 0) =
  311. for i in start..<n.len:
  312. var a = n[i]
  313. case a.kind:
  314. of nkCommentStmt: continue
  315. of nkIdentDefs, nkVarTuple, nkConstDef:
  316. semTemplIdentDef(c, a, symKind)
  317. else:
  318. illFormedAst(a, c.c.config)
  319. proc semPattern(c: PContext, n: PNode; s: PSym): PNode
  320. proc semTemplBodySons(c: var TemplCtx, n: PNode): PNode =
  321. result = n
  322. for i in 0..<n.len:
  323. result[i] = semTemplBody(c, n[i])
  324. proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
  325. result = n
  326. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  327. case n.kind
  328. of nkIdent:
  329. if n.ident.id in c.toInject: return n
  330. let s = qualifiedLookUp(c.c, n, {})
  331. if s != nil:
  332. if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags:
  333. incl(s.flags, sfUsed)
  334. result = newSymNode(s, n.info)
  335. onUse(n.info, s)
  336. elif contains(c.toBind, s.id):
  337. result = symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  338. elif contains(c.toMixin, s.name.id):
  339. result = symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  340. elif s.owner == c.owner and sfGenSym in s.flags and c.noGenSym == 0:
  341. # template tmp[T](x: var seq[T]) =
  342. # var yz: T
  343. incl(s.flags, sfUsed)
  344. result = newSymNode(s, n.info)
  345. onUse(n.info, s)
  346. else:
  347. if s.kind in {skType, skVar, skLet, skConst}:
  348. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  349. result = semTemplSymbol(c.c, n, s, c.noGenSym > 0)
  350. of nkBind:
  351. result = semTemplBody(c, n[0])
  352. of nkBindStmt:
  353. result = semBindStmt(c.c, n, c.toBind)
  354. of nkMixinStmt:
  355. if c.scopeN > 0: result = semTemplBodySons(c, n)
  356. else: result = semMixinStmt(c.c, n, c.toMixin)
  357. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  358. discard
  359. of nkIfStmt:
  360. for i in 0..<n.len:
  361. var it = n[i]
  362. if it.len == 2:
  363. openScope(c)
  364. it[0] = semTemplBody(c, it[0])
  365. it[1] = semTemplBody(c, it[1])
  366. closeScope(c)
  367. else:
  368. n[i] = semTemplBodyScope(c, it)
  369. of nkWhileStmt:
  370. openScope(c)
  371. for i in 0..<n.len:
  372. n[i] = semTemplBody(c, n[i])
  373. closeScope(c)
  374. of nkCaseStmt:
  375. openScope(c)
  376. n[0] = semTemplBody(c, n[0])
  377. for i in 1..<n.len:
  378. var a = n[i]
  379. checkMinSonsLen(a, 1, c.c.config)
  380. for j in 0..<a.len-1:
  381. a[j] = semTemplBody(c, a[j])
  382. a[^1] = semTemplBodyScope(c, a[^1])
  383. closeScope(c)
  384. of nkForStmt, nkParForStmt:
  385. openScope(c)
  386. n[^2] = semTemplBody(c, n[^2])
  387. for i in 0..<n.len - 2:
  388. if n[i].kind == nkVarTuple:
  389. for j in 0..<n[i].len-1:
  390. addLocalDecl(c, n[i][j], skForVar)
  391. else:
  392. addLocalDecl(c, n[i], skForVar)
  393. openScope(c)
  394. n[^1] = semTemplBody(c, n[^1])
  395. closeScope(c)
  396. closeScope(c)
  397. of nkBlockStmt, nkBlockExpr, nkBlockType:
  398. checkSonsLen(n, 2, c.c.config)
  399. openScope(c)
  400. if n[0].kind != nkEmpty:
  401. addLocalDecl(c, n[0], skLabel)
  402. when false:
  403. # labels are always 'gensym'ed:
  404. let s = newGenSym(skLabel, n[0], c)
  405. addPrelimDecl(c.c, s)
  406. styleCheckDef(c.c, s)
  407. onDef(n[0].info, s)
  408. n[0] = newSymNode(s, n[0].info)
  409. n[1] = semTemplBody(c, n[1])
  410. closeScope(c)
  411. of nkTryStmt, nkHiddenTryStmt:
  412. checkMinSonsLen(n, 2, c.c.config)
  413. n[0] = semTemplBodyScope(c, n[0])
  414. for i in 1..<n.len:
  415. var a = n[i]
  416. checkMinSonsLen(a, 1, c.c.config)
  417. openScope(c)
  418. for j in 0..<a.len-1:
  419. if a[j].isInfixAs():
  420. addLocalDecl(c, a[j][2], skLet)
  421. a[j][1] = semTemplBody(c, a[j][1])
  422. else:
  423. a[j] = semTemplBody(c, a[j])
  424. a[^1] = semTemplBodyScope(c, a[^1])
  425. closeScope(c)
  426. of nkVarSection: semTemplSomeDecl(c, n, skVar)
  427. of nkLetSection: semTemplSomeDecl(c, n, skLet)
  428. of nkFormalParams:
  429. checkMinSonsLen(n, 1, c.c.config)
  430. semTemplSomeDecl(c, n, skParam, 1)
  431. n[0] = semTemplBody(c, n[0])
  432. of nkConstSection: semTemplSomeDecl(c, n, skConst)
  433. of nkTypeSection:
  434. for i in 0..<n.len:
  435. var a = n[i]
  436. if a.kind == nkCommentStmt: continue
  437. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  438. checkSonsLen(a, 3, c.c.config)
  439. addLocalDecl(c, a[0], skType)
  440. for i in 0..<n.len:
  441. var a = n[i]
  442. if a.kind == nkCommentStmt: continue
  443. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  444. checkSonsLen(a, 3, c.c.config)
  445. if a[1].kind != nkEmpty:
  446. openScope(c)
  447. a[1] = semTemplBody(c, a[1])
  448. a[2] = semTemplBody(c, a[2])
  449. closeScope(c)
  450. else:
  451. a[2] = semTemplBody(c, a[2])
  452. of nkObjectTy:
  453. openScope(c)
  454. result = semTemplBodySons(c, n)
  455. closeScope(c)
  456. of nkRecList:
  457. for i in 0..<n.len:
  458. var a = n[i]
  459. case a.kind:
  460. of nkCommentStmt, nkNilLit, nkSym, nkEmpty: continue
  461. of nkIdentDefs:
  462. semTemplIdentDef(c, a, skField)
  463. of nkRecCase, nkRecWhen:
  464. n[i] = semTemplBody(c, a)
  465. else:
  466. illFormedAst(a, c.c.config)
  467. of nkRecCase:
  468. semTemplIdentDef(c, n[0], skField)
  469. for i in 1..<n.len:
  470. n[i] = semTemplBody(c, n[i])
  471. of nkProcDef, nkLambdaKinds:
  472. result = semRoutineInTemplBody(c, n, skProc)
  473. of nkFuncDef:
  474. result = semRoutineInTemplBody(c, n, skFunc)
  475. of nkMethodDef:
  476. result = semRoutineInTemplBody(c, n, skMethod)
  477. of nkIteratorDef:
  478. result = semRoutineInTemplBody(c, n, skIterator)
  479. of nkTemplateDef:
  480. result = semRoutineInTemplBody(c, n, skTemplate)
  481. of nkMacroDef:
  482. result = semRoutineInTemplBody(c, n, skMacro)
  483. of nkConverterDef:
  484. result = semRoutineInTemplBody(c, n, skConverter)
  485. of nkPragmaExpr:
  486. result[0] = semTemplBody(c, n[0])
  487. of nkPostfix:
  488. result[1] = semTemplBody(c, n[1])
  489. of nkPragma:
  490. for x in n:
  491. if x.kind == nkExprColonExpr:
  492. x[1] = semTemplBody(c, x[1])
  493. of nkBracketExpr:
  494. result = newNodeI(nkCall, n.info)
  495. result.add newIdentNode(getIdent(c.c.cache, "[]"), n.info)
  496. for i in 0..<n.len: result.add(n[i])
  497. result = semTemplBodySons(c, result)
  498. of nkCurlyExpr:
  499. result = newNodeI(nkCall, n.info)
  500. result.add newIdentNode(getIdent(c.c.cache, "{}"), n.info)
  501. for i in 0..<n.len: result.add(n[i])
  502. result = semTemplBodySons(c, result)
  503. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  504. checkSonsLen(n, 2, c.c.config)
  505. let a = n[0]
  506. let b = n[1]
  507. let k = a.kind
  508. case k
  509. of nkBracketExpr:
  510. result = newNodeI(nkCall, n.info)
  511. result.add newIdentNode(getIdent(c.c.cache, "[]="), n.info)
  512. for i in 0..<a.len: result.add(a[i])
  513. result.add(b)
  514. let a0 = semTemplBody(c, a[0])
  515. result = semTemplBodySons(c, result)
  516. of nkCurlyExpr:
  517. result = newNodeI(nkCall, n.info)
  518. result.add newIdentNode(getIdent(c.c.cache, "{}="), n.info)
  519. for i in 0..<a.len: result.add(a[i])
  520. result.add(b)
  521. result = semTemplBodySons(c, result)
  522. else:
  523. result = semTemplBodySons(c, n)
  524. of nkCallKinds-{nkPostfix}:
  525. # do not transform runnableExamples (bug #9143)
  526. if not isRunnableExamples(n[0]):
  527. result = semTemplBodySons(c, n)
  528. of nkDotExpr, nkAccQuoted:
  529. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  530. # so we use the generic code for nkDotExpr too
  531. let s = qualifiedLookUp(c.c, n, {})
  532. if s != nil:
  533. # do not symchoice a quoted template parameter (bug #2390):
  534. if s.owner == c.owner and s.kind == skParam and
  535. n.kind == nkAccQuoted and n.len == 1:
  536. incl(s.flags, sfUsed)
  537. onUse(n.info, s)
  538. return newSymNode(s, n.info)
  539. elif contains(c.toBind, s.id):
  540. return symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  541. elif contains(c.toMixin, s.name.id):
  542. return symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  543. else:
  544. return symChoice(c.c, n, s, scOpen, c.noGenSym > 0)
  545. if n.kind == nkDotExpr:
  546. result = n
  547. result[0] = semTemplBody(c, n[0])
  548. inc c.noGenSym
  549. result[1] = semTemplBody(c, n[1])
  550. dec c.noGenSym
  551. if result[1].kind == nkSym and result[1].sym.kind in routineKinds:
  552. # prevent `dotTransformation` from rewriting this node to `nkIdent`
  553. # by making it a symchoice
  554. # in generics this becomes `nkClosedSymChoice` but this breaks code
  555. # as the old behavior here was that this became `nkIdent`
  556. var choice = newNodeIT(nkOpenSymChoice, n[1].info, newTypeS(tyNone, c.c))
  557. choice.add result[1]
  558. result[1] = choice
  559. else:
  560. result = semTemplBodySons(c, n)
  561. of nkExprColonExpr, nkExprEqExpr:
  562. if n.len == 2:
  563. inc c.noGenSym
  564. result[0] = semTemplBody(c, n[0])
  565. dec c.noGenSym
  566. result[1] = semTemplBody(c, n[1])
  567. else:
  568. result = semTemplBodySons(c, n)
  569. of nkTableConstr:
  570. # also transform the keys (bug #12595)
  571. for i in 0..<n.len:
  572. result[i] = semTemplBodySons(c, n[i])
  573. else:
  574. result = semTemplBodySons(c, n)
  575. proc semTemplBodyDirty(c: var TemplCtx, n: PNode): PNode =
  576. result = n
  577. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  578. case n.kind
  579. of nkIdent:
  580. let s = qualifiedLookUp(c.c, n, {})
  581. if s != nil:
  582. if s.owner == c.owner and s.kind == skParam:
  583. result = newSymNode(s, n.info)
  584. elif contains(c.toBind, s.id):
  585. result = symChoice(c.c, n, s, scClosed)
  586. of nkBind:
  587. result = semTemplBodyDirty(c, n[0])
  588. of nkBindStmt:
  589. result = semBindStmt(c.c, n, c.toBind)
  590. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  591. discard
  592. else:
  593. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  594. # so we use the generic code for nkDotExpr too
  595. if n.kind == nkDotExpr or n.kind == nkAccQuoted:
  596. let s = qualifiedLookUp(c.c, n, {})
  597. if s != nil and contains(c.toBind, s.id):
  598. return symChoice(c.c, n, s, scClosed)
  599. result = n
  600. for i in 0..<n.len:
  601. result[i] = semTemplBodyDirty(c, n[i])
  602. # in semstmts.nim:
  603. proc semProcAnnotation(c: PContext, prc: PNode; validPragmas: TSpecialWords): PNode
  604. proc semTemplateDef(c: PContext, n: PNode): PNode =
  605. result = semProcAnnotation(c, n, templatePragmas)
  606. if result != nil: return result
  607. result = n
  608. var s: PSym
  609. if isTopLevel(c):
  610. s = semIdentVis(c, skTemplate, n[namePos], {sfExported})
  611. incl(s.flags, sfGlobal)
  612. else:
  613. s = semIdentVis(c, skTemplate, n[namePos], {})
  614. assert s.kind == skTemplate
  615. styleCheckDef(c, s)
  616. onDef(n[namePos].info, s)
  617. # check parameter list:
  618. #s.scope = c.currentScope
  619. # push noalias flag at first to prevent unwanted recursive calls:
  620. incl(s.flags, sfNoalias)
  621. pushOwner(c, s)
  622. openScope(c)
  623. n[namePos] = newSymNode(s)
  624. pragmaCallable(c, s, n, templatePragmas)
  625. implicitPragmas(c, s, n.info, templatePragmas)
  626. setGenericParamsMisc(c, n)
  627. # process parameters:
  628. var allUntyped = true
  629. var nullary = true
  630. if n[paramsPos].kind != nkEmpty:
  631. semParamList(c, n[paramsPos], n[genericParamsPos], s)
  632. # a template's parameters are not gensym'ed even if that was originally the
  633. # case as we determine whether it's a template parameter in the template
  634. # body by the absence of the sfGenSym flag:
  635. for i in 1..<s.typ.n.len:
  636. let param = s.typ.n[i].sym
  637. if param.name.id != ord(wUnderscore):
  638. param.flags.incl sfTemplateParam
  639. param.flags.excl sfGenSym
  640. if param.typ.kind != tyUntyped: allUntyped = false
  641. # no default value, parameters required in call
  642. if param.ast == nil: nullary = false
  643. else:
  644. s.typ = newTypeS(tyProc, c)
  645. # XXX why do we need tyTyped as a return type again?
  646. s.typ.n = newNodeI(nkFormalParams, n.info)
  647. rawAddSon(s.typ, newTypeS(tyTyped, c))
  648. s.typ.n.add newNodeIT(nkType, n.info, s.typ[0])
  649. if n[genericParamsPos].safeLen == 0:
  650. # restore original generic type params as no explicit or implicit were found
  651. n[genericParamsPos] = n[miscPos][1]
  652. n[miscPos] = c.graph.emptyNode
  653. if allUntyped: incl(s.flags, sfAllUntyped)
  654. if nullary and
  655. n[genericParamsPos].kind == nkEmpty and
  656. n[bodyPos].kind != nkEmpty:
  657. # template can be called with alias syntax, remove pushed noalias flag
  658. excl(s.flags, sfNoalias)
  659. if n[patternPos].kind != nkEmpty:
  660. n[patternPos] = semPattern(c, n[patternPos], s)
  661. var ctx: TemplCtx
  662. ctx.toBind = initIntSet()
  663. ctx.toMixin = initIntSet()
  664. ctx.toInject = initIntSet()
  665. ctx.c = c
  666. ctx.owner = s
  667. if sfDirty in s.flags:
  668. n[bodyPos] = semTemplBodyDirty(ctx, n[bodyPos])
  669. else:
  670. n[bodyPos] = semTemplBody(ctx, n[bodyPos])
  671. # only parameters are resolved, no type checking is performed
  672. semIdeForTemplateOrGeneric(c, n[bodyPos], ctx.cursorInBody)
  673. closeScope(c)
  674. popOwner(c)
  675. # set the symbol AST after pragmas, at least. This stops pragma that have
  676. # been pushed (implicit) to be explicitly added to the template definition
  677. # and misapplied to the body. see #18113
  678. s.ast = n
  679. if sfCustomPragma in s.flags:
  680. if n[bodyPos].kind != nkEmpty:
  681. localError(c.config, n[bodyPos].info, errImplOfXNotAllowed % s.name.s)
  682. elif n[bodyPos].kind == nkEmpty:
  683. localError(c.config, n.info, "implementation of '$1' expected" % s.name.s)
  684. var (proto, comesFromShadowscope) = searchForProc(c, c.currentScope, s)
  685. if proto == nil:
  686. addInterfaceOverloadableSymAt(c, c.currentScope, s)
  687. elif not comesFromShadowscope:
  688. if {sfTemplateRedefinition, sfGenSym} * s.flags == {}:
  689. #wrongRedefinition(c, n.info, proto.name.s, proto.info)
  690. message(c.config, n.info, warnImplicitTemplateRedefinition, s.name.s)
  691. symTabReplace(c.currentScope.symbols, proto, s)
  692. if n[patternPos].kind != nkEmpty:
  693. c.patterns.add(s)
  694. proc semPatternBody(c: var TemplCtx, n: PNode): PNode =
  695. template templToExpand(s: untyped): untyped =
  696. s.kind == skTemplate and (s.typ.len == 1 or sfAllUntyped in s.flags)
  697. proc newParam(c: var TemplCtx, n: PNode, s: PSym): PNode =
  698. # the param added in the current scope is actually wrong here for
  699. # macros because they have a shadowed param of type 'PNimNode' (see
  700. # semtypes.addParamOrResult). Within the pattern we have to ensure
  701. # to use the param with the proper type though:
  702. incl(s.flags, sfUsed)
  703. onUse(n.info, s)
  704. let x = c.owner.typ.n[s.position+1].sym
  705. assert x.name == s.name
  706. result = newSymNode(x, n.info)
  707. proc handleSym(c: var TemplCtx, n: PNode, s: PSym): PNode =
  708. result = n
  709. if s != nil:
  710. if s.owner == c.owner and s.kind == skParam:
  711. result = newParam(c, n, s)
  712. elif contains(c.toBind, s.id):
  713. result = symChoice(c.c, n, s, scClosed)
  714. elif templToExpand(s):
  715. result = semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  716. else:
  717. discard
  718. # we keep the ident unbound for matching instantiated symbols and
  719. # more flexibility
  720. proc expectParam(c: var TemplCtx, n: PNode): PNode =
  721. let s = qualifiedLookUp(c.c, n, {})
  722. if s != nil and s.owner == c.owner and s.kind == skParam:
  723. result = newParam(c, n, s)
  724. else:
  725. localError(c.c.config, n.info, "invalid expression")
  726. result = n
  727. result = n
  728. case n.kind
  729. of nkIdent:
  730. let s = qualifiedLookUp(c.c, n, {})
  731. result = handleSym(c, n, s)
  732. of nkBindStmt:
  733. result = semBindStmt(c.c, n, c.toBind)
  734. of nkEmpty, nkSym..nkNilLit: discard
  735. of nkCurlyExpr:
  736. # we support '(pattern){x}' to bind a subpattern to a parameter 'x';
  737. # '(pattern){|x}' does the same but the matches will be gathered in 'x'
  738. if n.len != 2:
  739. localError(c.c.config, n.info, "invalid expression")
  740. elif n[1].kind == nkIdent:
  741. n[0] = semPatternBody(c, n[0])
  742. n[1] = expectParam(c, n[1])
  743. elif n[1].kind == nkPrefix and n[1][0].kind == nkIdent:
  744. let opr = n[1][0]
  745. if opr.ident.s == "|":
  746. n[0] = semPatternBody(c, n[0])
  747. n[1][1] = expectParam(c, n[1][1])
  748. else:
  749. localError(c.c.config, n.info, "invalid expression")
  750. else:
  751. localError(c.c.config, n.info, "invalid expression")
  752. of nkStmtList, nkStmtListExpr:
  753. if stupidStmtListExpr(n):
  754. result = semPatternBody(c, n.lastSon)
  755. else:
  756. for i in 0..<n.len:
  757. result[i] = semPatternBody(c, n[i])
  758. of nkCallKinds:
  759. let s = qualifiedLookUp(c.c, n[0], {})
  760. if s != nil:
  761. if s.owner == c.owner and s.kind == skParam: discard
  762. elif contains(c.toBind, s.id): discard
  763. elif templToExpand(s):
  764. return semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  765. if n.kind == nkInfix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  766. # we interpret `*` and `|` only as pattern operators if they occur in
  767. # infix notation, so that '`*`(a, b)' can be used for verbatim matching:
  768. if id.s == "*" or id.s == "**":
  769. result = newNodeI(nkPattern, n.info, n.len)
  770. result[0] = newIdentNode(id, n.info)
  771. result[1] = semPatternBody(c, n[1])
  772. result[2] = expectParam(c, n[2])
  773. return
  774. elif id.s == "|":
  775. result = newNodeI(nkPattern, n.info, n.len)
  776. result[0] = newIdentNode(id, n.info)
  777. result[1] = semPatternBody(c, n[1])
  778. result[2] = semPatternBody(c, n[2])
  779. return
  780. if n.kind == nkPrefix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  781. if id.s == "~":
  782. result = newNodeI(nkPattern, n.info, n.len)
  783. result[0] = newIdentNode(id, n.info)
  784. result[1] = semPatternBody(c, n[1])
  785. return
  786. for i in 0..<n.len:
  787. result[i] = semPatternBody(c, n[i])
  788. else:
  789. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  790. # so we use the generic code for nkDotExpr too
  791. case n.kind
  792. of nkDotExpr, nkAccQuoted:
  793. let s = qualifiedLookUp(c.c, n, {})
  794. if s != nil:
  795. if contains(c.toBind, s.id):
  796. return symChoice(c.c, n, s, scClosed)
  797. else:
  798. return newIdentNode(s.name, n.info)
  799. of nkPar:
  800. if n.len == 1: return semPatternBody(c, n[0])
  801. else: discard
  802. for i in 0..<n.len:
  803. result[i] = semPatternBody(c, n[i])
  804. proc semPattern(c: PContext, n: PNode; s: PSym): PNode =
  805. openScope(c)
  806. var ctx: TemplCtx
  807. ctx.toBind = initIntSet()
  808. ctx.toMixin = initIntSet()
  809. ctx.toInject = initIntSet()
  810. ctx.c = c
  811. ctx.owner = getCurrOwner(c)
  812. result = flattenStmts(semPatternBody(ctx, n))
  813. if result.kind in {nkStmtList, nkStmtListExpr}:
  814. if result.len == 1:
  815. result = result[0]
  816. elif result.len == 0:
  817. localError(c.config, n.info, "a pattern cannot be empty")
  818. closeScope(c)
  819. addPattern(c, LazySym(sym: s))