semtempl.nim 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. else:
  63. # semantic checking requires a type; ``fitNode`` deals with it
  64. # appropriately
  65. let kind = if r == scClosed or n.kind == nkDotExpr: nkClosedSymChoice
  66. else: nkOpenSymChoice
  67. result = newNodeIT(kind, info, newTypeS(tyNone, c))
  68. a = initOverloadIter(o, c, n)
  69. while a != nil:
  70. if a.kind != skModule and (not isField or sfGenSym notin a.flags):
  71. incl(a.flags, sfUsed)
  72. markOwnerModuleAsUsed(c, a)
  73. result.add newSymNode(a, info)
  74. onUse(info, a)
  75. a = nextOverloadIter(o, c, n)
  76. proc semBindStmt(c: PContext, n: PNode, toBind: var IntSet): PNode =
  77. result = copyNode(n)
  78. for i in 0..<n.len:
  79. var a = n[i]
  80. # If 'a' is an overloaded symbol, we used to use the first symbol
  81. # as a 'witness' and use the fact that subsequent lookups will yield
  82. # the same symbol!
  83. # This is however not true anymore for hygienic templates as semantic
  84. # processing for them changes the symbol table...
  85. let s = qualifiedLookUp(c, a, {checkUndeclared})
  86. if s != nil:
  87. # we need to mark all symbols:
  88. let sc = symChoice(c, n, s, scClosed)
  89. if sc.kind == nkSym:
  90. toBind.incl(sc.sym.id)
  91. result.add sc
  92. else:
  93. for x in items(sc):
  94. toBind.incl(x.sym.id)
  95. result.add x
  96. else:
  97. illFormedAst(a, c.config)
  98. proc semMixinStmt(c: PContext, n: PNode, toMixin: var IntSet): PNode =
  99. result = copyNode(n)
  100. var count = 0
  101. for i in 0..<n.len:
  102. toMixin.incl(considerQuotedIdent(c, n[i]).id)
  103. let x = symChoice(c, n[i], nil, scForceOpen)
  104. inc count, x.len
  105. result.add x
  106. if count == 0:
  107. result = newNodeI(nkEmpty, n.info)
  108. proc replaceIdentBySym(c: PContext; n: var PNode, s: PNode) =
  109. case n.kind
  110. of nkPostfix: replaceIdentBySym(c, n[1], s)
  111. of nkPragmaExpr: replaceIdentBySym(c, n[0], s)
  112. of nkIdent, nkAccQuoted, nkSym: n = s
  113. else: illFormedAst(n, c.config)
  114. type
  115. TemplCtx = object
  116. c: PContext
  117. toBind, toMixin, toInject: IntSet
  118. owner: PSym
  119. cursorInBody: bool # only for nimsuggest
  120. scopeN: int
  121. noGenSym: int
  122. inTemplateHeader: int
  123. proc getIdentNode(c: var TemplCtx, n: PNode): PNode =
  124. case n.kind
  125. of nkPostfix: result = getIdentNode(c, n[1])
  126. of nkPragmaExpr: result = getIdentNode(c, n[0])
  127. of nkIdent:
  128. result = n
  129. let s = qualifiedLookUp(c.c, n, {})
  130. if s != nil:
  131. if s.owner == c.owner and s.kind == skParam:
  132. result = newSymNode(s, n.info)
  133. of nkAccQuoted, nkSym: result = n
  134. else:
  135. illFormedAst(n, c.c.config)
  136. result = n
  137. proc isTemplParam(c: TemplCtx, n: PNode): bool {.inline.} =
  138. result = n.kind == nkSym and n.sym.kind == skParam and
  139. n.sym.owner == c.owner and sfTemplateParam in n.sym.flags
  140. proc semTemplBody(c: var TemplCtx, n: PNode): PNode
  141. proc openScope(c: var TemplCtx) =
  142. openScope(c.c)
  143. proc closeScope(c: var TemplCtx) =
  144. closeScope(c.c)
  145. proc semTemplBodyScope(c: var TemplCtx, n: PNode): PNode =
  146. openScope(c)
  147. result = semTemplBody(c, n)
  148. closeScope(c)
  149. proc onlyReplaceParams(c: var TemplCtx, n: PNode): PNode =
  150. result = n
  151. if n.kind == nkIdent:
  152. let s = qualifiedLookUp(c.c, n, {})
  153. if s != nil:
  154. if s.owner == c.owner and s.kind == skParam:
  155. incl(s.flags, sfUsed)
  156. result = newSymNode(s, n.info)
  157. onUse(n.info, s)
  158. else:
  159. for i in 0..<n.safeLen:
  160. result[i] = onlyReplaceParams(c, n[i])
  161. proc newGenSym(kind: TSymKind, n: PNode, c: var TemplCtx): PSym =
  162. result = newSym(kind, considerQuotedIdent(c.c, n), nextSymId c.c.idgen, c.owner, n.info)
  163. incl(result.flags, sfGenSym)
  164. incl(result.flags, sfShadowed)
  165. proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
  166. # locals default to 'gensym':
  167. if n.kind == nkPragmaExpr and symBinding(n[1]) == spInject:
  168. # even if injected, don't produce a sym choice here:
  169. #n = semTemplBody(c, n)
  170. var x = n[0]
  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. 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.s == "_"):
  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 semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start = 0) =
  301. for i in start..<n.len:
  302. var a = n[i]
  303. case a.kind:
  304. of nkCommentStmt: continue
  305. of nkIdentDefs, nkVarTuple, nkConstDef:
  306. checkMinSonsLen(a, 3, c.c.config)
  307. when defined(nimsuggest):
  308. inc c.c.inTypeContext
  309. a[^2] = semTemplBody(c, a[^2])
  310. when defined(nimsuggest):
  311. dec c.c.inTypeContext
  312. a[^1] = semTemplBody(c, a[^1])
  313. for j in 0..<a.len-2:
  314. addLocalDecl(c, a[j], symKind)
  315. else:
  316. illFormedAst(a, c.c.config)
  317. proc semPattern(c: PContext, n: PNode; s: PSym): PNode
  318. proc semTemplBodySons(c: var TemplCtx, n: PNode): PNode =
  319. result = n
  320. for i in 0..<n.len:
  321. result[i] = semTemplBody(c, n[i])
  322. proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
  323. result = n
  324. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  325. case n.kind
  326. of nkIdent:
  327. if n.ident.id in c.toInject: return n
  328. let s = qualifiedLookUp(c.c, n, {})
  329. if s != nil:
  330. if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags:
  331. incl(s.flags, sfUsed)
  332. result = newSymNode(s, n.info)
  333. onUse(n.info, s)
  334. elif contains(c.toBind, s.id):
  335. result = symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  336. elif contains(c.toMixin, s.name.id):
  337. result = symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  338. elif s.owner == c.owner and sfGenSym in s.flags and c.noGenSym == 0:
  339. # template tmp[T](x: var seq[T]) =
  340. # var yz: T
  341. incl(s.flags, sfUsed)
  342. result = newSymNode(s, n.info)
  343. onUse(n.info, s)
  344. else:
  345. if s.kind in {skType, skVar, skLet, skConst}:
  346. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  347. result = semTemplSymbol(c.c, n, s, c.noGenSym > 0)
  348. of nkBind:
  349. result = semTemplBody(c, n[0])
  350. of nkBindStmt:
  351. result = semBindStmt(c.c, n, c.toBind)
  352. of nkMixinStmt:
  353. if c.scopeN > 0: result = semTemplBodySons(c, n)
  354. else: result = semMixinStmt(c.c, n, c.toMixin)
  355. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  356. discard
  357. of nkIfStmt:
  358. for i in 0..<n.len:
  359. var it = n[i]
  360. if it.len == 2:
  361. openScope(c)
  362. it[0] = semTemplBody(c, it[0])
  363. it[1] = semTemplBody(c, it[1])
  364. closeScope(c)
  365. else:
  366. n[i] = semTemplBodyScope(c, it)
  367. of nkWhileStmt:
  368. openScope(c)
  369. for i in 0..<n.len:
  370. n[i] = semTemplBody(c, n[i])
  371. closeScope(c)
  372. of nkCaseStmt:
  373. openScope(c)
  374. n[0] = semTemplBody(c, n[0])
  375. for i in 1..<n.len:
  376. var a = n[i]
  377. checkMinSonsLen(a, 1, c.c.config)
  378. for j in 0..<a.len-1:
  379. a[j] = semTemplBody(c, a[j])
  380. a[^1] = semTemplBodyScope(c, a[^1])
  381. closeScope(c)
  382. of nkForStmt, nkParForStmt:
  383. openScope(c)
  384. n[^2] = semTemplBody(c, n[^2])
  385. for i in 0..<n.len - 2:
  386. if n[i].kind == nkVarTuple:
  387. for j in 0..<n[i].len-1:
  388. addLocalDecl(c, n[i][j], skForVar)
  389. else:
  390. addLocalDecl(c, n[i], skForVar)
  391. openScope(c)
  392. n[^1] = semTemplBody(c, n[^1])
  393. closeScope(c)
  394. closeScope(c)
  395. of nkBlockStmt, nkBlockExpr, nkBlockType:
  396. checkSonsLen(n, 2, c.c.config)
  397. openScope(c)
  398. if n[0].kind != nkEmpty:
  399. addLocalDecl(c, n[0], skLabel)
  400. when false:
  401. # labels are always 'gensym'ed:
  402. let s = newGenSym(skLabel, n[0], c)
  403. addPrelimDecl(c.c, s)
  404. styleCheckDef(c.c, s)
  405. onDef(n[0].info, s)
  406. n[0] = newSymNode(s, n[0].info)
  407. n[1] = semTemplBody(c, n[1])
  408. closeScope(c)
  409. of nkTryStmt, nkHiddenTryStmt:
  410. checkMinSonsLen(n, 2, c.c.config)
  411. n[0] = semTemplBodyScope(c, n[0])
  412. for i in 1..<n.len:
  413. var a = n[i]
  414. checkMinSonsLen(a, 1, c.c.config)
  415. openScope(c)
  416. for j in 0..<a.len-1:
  417. if a[j].isInfixAs():
  418. addLocalDecl(c, a[j][2], skLet)
  419. a[j][1] = semTemplBody(c, a[j][1])
  420. else:
  421. a[j] = semTemplBody(c, a[j])
  422. a[^1] = semTemplBodyScope(c, a[^1])
  423. closeScope(c)
  424. of nkVarSection: semTemplSomeDecl(c, n, skVar)
  425. of nkLetSection: semTemplSomeDecl(c, n, skLet)
  426. of nkFormalParams:
  427. checkMinSonsLen(n, 1, c.c.config)
  428. semTemplSomeDecl(c, n, skParam, 1)
  429. n[0] = semTemplBody(c, n[0])
  430. of nkConstSection: semTemplSomeDecl(c, n, skConst)
  431. of nkTypeSection:
  432. for i in 0..<n.len:
  433. var a = n[i]
  434. if a.kind == nkCommentStmt: continue
  435. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  436. checkSonsLen(a, 3, c.c.config)
  437. addLocalDecl(c, a[0], skType)
  438. for i in 0..<n.len:
  439. var a = n[i]
  440. if a.kind == nkCommentStmt: continue
  441. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  442. checkSonsLen(a, 3, c.c.config)
  443. if a[1].kind != nkEmpty:
  444. openScope(c)
  445. a[1] = semTemplBody(c, a[1])
  446. a[2] = semTemplBody(c, a[2])
  447. closeScope(c)
  448. else:
  449. a[2] = semTemplBody(c, a[2])
  450. of nkProcDef, nkLambdaKinds:
  451. result = semRoutineInTemplBody(c, n, skProc)
  452. of nkFuncDef:
  453. result = semRoutineInTemplBody(c, n, skFunc)
  454. of nkMethodDef:
  455. result = semRoutineInTemplBody(c, n, skMethod)
  456. of nkIteratorDef:
  457. result = semRoutineInTemplBody(c, n, skIterator)
  458. of nkTemplateDef:
  459. result = semRoutineInTemplBody(c, n, skTemplate)
  460. of nkMacroDef:
  461. result = semRoutineInTemplBody(c, n, skMacro)
  462. of nkConverterDef:
  463. result = semRoutineInTemplBody(c, n, skConverter)
  464. of nkPragmaExpr:
  465. result[0] = semTemplBody(c, n[0])
  466. of nkPostfix:
  467. result[1] = semTemplBody(c, n[1])
  468. of nkPragma:
  469. for x in n:
  470. if x.kind == nkExprColonExpr:
  471. x[1] = semTemplBody(c, x[1])
  472. of nkBracketExpr:
  473. result = newNodeI(nkCall, n.info)
  474. result.add newIdentNode(getIdent(c.c.cache, "[]"), n.info)
  475. for i in 0..<n.len: result.add(n[i])
  476. result = semTemplBodySons(c, result)
  477. of nkCurlyExpr:
  478. result = newNodeI(nkCall, n.info)
  479. result.add newIdentNode(getIdent(c.c.cache, "{}"), n.info)
  480. for i in 0..<n.len: result.add(n[i])
  481. result = semTemplBodySons(c, result)
  482. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  483. checkSonsLen(n, 2, c.c.config)
  484. let a = n[0]
  485. let b = n[1]
  486. let k = a.kind
  487. case k
  488. of nkBracketExpr:
  489. result = newNodeI(nkCall, n.info)
  490. result.add newIdentNode(getIdent(c.c.cache, "[]="), n.info)
  491. for i in 0..<a.len: result.add(a[i])
  492. result.add(b)
  493. let a0 = semTemplBody(c, a[0])
  494. result = semTemplBodySons(c, result)
  495. of nkCurlyExpr:
  496. result = newNodeI(nkCall, n.info)
  497. result.add newIdentNode(getIdent(c.c.cache, "{}="), n.info)
  498. for i in 0..<a.len: result.add(a[i])
  499. result.add(b)
  500. result = semTemplBodySons(c, result)
  501. else:
  502. result = semTemplBodySons(c, n)
  503. of nkCallKinds-{nkPostfix}:
  504. # do not transform runnableExamples (bug #9143)
  505. if not isRunnableExamples(n[0]):
  506. result = semTemplBodySons(c, n)
  507. of nkDotExpr, nkAccQuoted:
  508. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  509. # so we use the generic code for nkDotExpr too
  510. let s = qualifiedLookUp(c.c, n, {})
  511. if s != nil:
  512. # do not symchoice a quoted template parameter (bug #2390):
  513. if s.owner == c.owner and s.kind == skParam and
  514. n.kind == nkAccQuoted and n.len == 1:
  515. incl(s.flags, sfUsed)
  516. onUse(n.info, s)
  517. return newSymNode(s, n.info)
  518. elif contains(c.toBind, s.id):
  519. return symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  520. elif contains(c.toMixin, s.name.id):
  521. return symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  522. else:
  523. return symChoice(c.c, n, s, scOpen, c.noGenSym > 0)
  524. if n.kind == nkDotExpr:
  525. result = n
  526. result[0] = semTemplBody(c, n[0])
  527. inc c.noGenSym
  528. result[1] = semTemplBody(c, n[1])
  529. dec c.noGenSym
  530. else:
  531. result = semTemplBodySons(c, n)
  532. of nkExprColonExpr, nkExprEqExpr:
  533. if n.len == 2:
  534. inc c.noGenSym
  535. result[0] = semTemplBody(c, n[0])
  536. dec c.noGenSym
  537. result[1] = semTemplBody(c, n[1])
  538. else:
  539. result = semTemplBodySons(c, n)
  540. of nkTableConstr:
  541. # also transform the keys (bug #12595)
  542. for i in 0..<n.len:
  543. result[i] = semTemplBodySons(c, n[i])
  544. else:
  545. result = semTemplBodySons(c, n)
  546. proc semTemplBodyDirty(c: var TemplCtx, n: PNode): PNode =
  547. result = n
  548. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  549. case n.kind
  550. of nkIdent:
  551. let s = qualifiedLookUp(c.c, n, {})
  552. if s != nil:
  553. if s.owner == c.owner and s.kind == skParam:
  554. result = newSymNode(s, n.info)
  555. elif contains(c.toBind, s.id):
  556. result = symChoice(c.c, n, s, scClosed)
  557. of nkBind:
  558. result = semTemplBodyDirty(c, n[0])
  559. of nkBindStmt:
  560. result = semBindStmt(c.c, n, c.toBind)
  561. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  562. discard
  563. else:
  564. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  565. # so we use the generic code for nkDotExpr too
  566. if n.kind == nkDotExpr or n.kind == nkAccQuoted:
  567. let s = qualifiedLookUp(c.c, n, {})
  568. if s != nil and contains(c.toBind, s.id):
  569. return symChoice(c.c, n, s, scClosed)
  570. result = n
  571. for i in 0..<n.len:
  572. result[i] = semTemplBodyDirty(c, n[i])
  573. # in semstmts.nim:
  574. proc semProcAnnotation(c: PContext, prc: PNode; validPragmas: TSpecialWords): PNode
  575. proc semTemplateDef(c: PContext, n: PNode): PNode =
  576. result = semProcAnnotation(c, n, templatePragmas)
  577. if result != nil: return result
  578. result = n
  579. var s: PSym
  580. if isTopLevel(c):
  581. s = semIdentVis(c, skTemplate, n[namePos], {sfExported})
  582. incl(s.flags, sfGlobal)
  583. else:
  584. s = semIdentVis(c, skTemplate, n[namePos], {})
  585. assert s.kind == skTemplate
  586. styleCheckDef(c, s)
  587. onDef(n[namePos].info, s)
  588. # check parameter list:
  589. #s.scope = c.currentScope
  590. pushOwner(c, s)
  591. openScope(c)
  592. n[namePos] = newSymNode(s)
  593. pragmaCallable(c, s, n, templatePragmas)
  594. implicitPragmas(c, s, n.info, templatePragmas)
  595. setGenericParamsMisc(c, n)
  596. # process parameters:
  597. var allUntyped = true
  598. if n[paramsPos].kind != nkEmpty:
  599. semParamList(c, n[paramsPos], n[genericParamsPos], s)
  600. # a template's parameters are not gensym'ed even if that was originally the
  601. # case as we determine whether it's a template parameter in the template
  602. # body by the absence of the sfGenSym flag:
  603. for i in 1..<s.typ.n.len:
  604. let param = s.typ.n[i].sym
  605. param.flags.incl sfTemplateParam
  606. param.flags.excl sfGenSym
  607. if param.typ.kind != tyUntyped: allUntyped = false
  608. else:
  609. s.typ = newTypeS(tyProc, c)
  610. # XXX why do we need tyTyped as a return type again?
  611. s.typ.n = newNodeI(nkFormalParams, n.info)
  612. rawAddSon(s.typ, newTypeS(tyTyped, c))
  613. s.typ.n.add newNodeIT(nkType, n.info, s.typ[0])
  614. if n[genericParamsPos].safeLen == 0:
  615. # restore original generic type params as no explicit or implicit were found
  616. n[genericParamsPos] = n[miscPos][1]
  617. n[miscPos] = c.graph.emptyNode
  618. if allUntyped: incl(s.flags, sfAllUntyped)
  619. if n[patternPos].kind != nkEmpty:
  620. n[patternPos] = semPattern(c, n[patternPos], s)
  621. var ctx: TemplCtx
  622. ctx.toBind = initIntSet()
  623. ctx.toMixin = initIntSet()
  624. ctx.toInject = initIntSet()
  625. ctx.c = c
  626. ctx.owner = s
  627. if sfDirty in s.flags:
  628. n[bodyPos] = semTemplBodyDirty(ctx, n[bodyPos])
  629. else:
  630. n[bodyPos] = semTemplBody(ctx, n[bodyPos])
  631. # only parameters are resolved, no type checking is performed
  632. semIdeForTemplateOrGeneric(c, n[bodyPos], ctx.cursorInBody)
  633. closeScope(c)
  634. popOwner(c)
  635. # set the symbol AST after pragmas, at least. This stops pragma that have
  636. # been pushed (implicit) to be explicitly added to the template definition
  637. # and misapplied to the body. see #18113
  638. s.ast = n
  639. if sfCustomPragma in s.flags:
  640. if n[bodyPos].kind != nkEmpty:
  641. localError(c.config, n[bodyPos].info, errImplOfXNotAllowed % s.name.s)
  642. elif n[bodyPos].kind == nkEmpty:
  643. localError(c.config, n.info, "implementation of '$1' expected" % s.name.s)
  644. var (proto, comesFromShadowscope) = searchForProc(c, c.currentScope, s)
  645. if proto == nil:
  646. addInterfaceOverloadableSymAt(c, c.currentScope, s)
  647. elif not comesFromShadowscope:
  648. if {sfTemplateRedefinition, sfGenSym} * s.flags == {}:
  649. #wrongRedefinition(c, n.info, proto.name.s, proto.info)
  650. message(c.config, n.info, warnTemplateRedefinition, s.name.s)
  651. symTabReplace(c.currentScope.symbols, proto, s)
  652. if n[patternPos].kind != nkEmpty:
  653. c.patterns.add(s)
  654. proc semPatternBody(c: var TemplCtx, n: PNode): PNode =
  655. template templToExpand(s: untyped): untyped =
  656. s.kind == skTemplate and (s.typ.len == 1 or sfAllUntyped in s.flags)
  657. proc newParam(c: var TemplCtx, n: PNode, s: PSym): PNode =
  658. # the param added in the current scope is actually wrong here for
  659. # macros because they have a shadowed param of type 'PNimNode' (see
  660. # semtypes.addParamOrResult). Within the pattern we have to ensure
  661. # to use the param with the proper type though:
  662. incl(s.flags, sfUsed)
  663. onUse(n.info, s)
  664. let x = c.owner.typ.n[s.position+1].sym
  665. assert x.name == s.name
  666. result = newSymNode(x, n.info)
  667. proc handleSym(c: var TemplCtx, n: PNode, s: PSym): PNode =
  668. result = n
  669. if s != nil:
  670. if s.owner == c.owner and s.kind == skParam:
  671. result = newParam(c, n, s)
  672. elif contains(c.toBind, s.id):
  673. result = symChoice(c.c, n, s, scClosed)
  674. elif templToExpand(s):
  675. result = semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  676. else:
  677. discard
  678. # we keep the ident unbound for matching instantiated symbols and
  679. # more flexibility
  680. proc expectParam(c: var TemplCtx, n: PNode): PNode =
  681. let s = qualifiedLookUp(c.c, n, {})
  682. if s != nil and s.owner == c.owner and s.kind == skParam:
  683. result = newParam(c, n, s)
  684. else:
  685. localError(c.c.config, n.info, "invalid expression")
  686. result = n
  687. result = n
  688. case n.kind
  689. of nkIdent:
  690. let s = qualifiedLookUp(c.c, n, {})
  691. result = handleSym(c, n, s)
  692. of nkBindStmt:
  693. result = semBindStmt(c.c, n, c.toBind)
  694. of nkEmpty, nkSym..nkNilLit: discard
  695. of nkCurlyExpr:
  696. # we support '(pattern){x}' to bind a subpattern to a parameter 'x';
  697. # '(pattern){|x}' does the same but the matches will be gathered in 'x'
  698. if n.len != 2:
  699. localError(c.c.config, n.info, "invalid expression")
  700. elif n[1].kind == nkIdent:
  701. n[0] = semPatternBody(c, n[0])
  702. n[1] = expectParam(c, n[1])
  703. elif n[1].kind == nkPrefix and n[1][0].kind == nkIdent:
  704. let opr = n[1][0]
  705. if opr.ident.s == "|":
  706. n[0] = semPatternBody(c, n[0])
  707. n[1][1] = expectParam(c, n[1][1])
  708. else:
  709. localError(c.c.config, n.info, "invalid expression")
  710. else:
  711. localError(c.c.config, n.info, "invalid expression")
  712. of nkStmtList, nkStmtListExpr:
  713. if stupidStmtListExpr(n):
  714. result = semPatternBody(c, n.lastSon)
  715. else:
  716. for i in 0..<n.len:
  717. result[i] = semPatternBody(c, n[i])
  718. of nkCallKinds:
  719. let s = qualifiedLookUp(c.c, n[0], {})
  720. if s != nil:
  721. if s.owner == c.owner and s.kind == skParam: discard
  722. elif contains(c.toBind, s.id): discard
  723. elif templToExpand(s):
  724. return semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  725. if n.kind == nkInfix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  726. # we interpret `*` and `|` only as pattern operators if they occur in
  727. # infix notation, so that '`*`(a, b)' can be used for verbatim matching:
  728. if id.s == "*" or id.s == "**":
  729. result = newNodeI(nkPattern, n.info, n.len)
  730. result[0] = newIdentNode(id, n.info)
  731. result[1] = semPatternBody(c, n[1])
  732. result[2] = expectParam(c, n[2])
  733. return
  734. elif id.s == "|":
  735. result = newNodeI(nkPattern, n.info, n.len)
  736. result[0] = newIdentNode(id, n.info)
  737. result[1] = semPatternBody(c, n[1])
  738. result[2] = semPatternBody(c, n[2])
  739. return
  740. if n.kind == nkPrefix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  741. if id.s == "~":
  742. result = newNodeI(nkPattern, n.info, n.len)
  743. result[0] = newIdentNode(id, n.info)
  744. result[1] = semPatternBody(c, n[1])
  745. return
  746. for i in 0..<n.len:
  747. result[i] = semPatternBody(c, n[i])
  748. else:
  749. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  750. # so we use the generic code for nkDotExpr too
  751. case n.kind
  752. of nkDotExpr, nkAccQuoted:
  753. let s = qualifiedLookUp(c.c, n, {})
  754. if s != nil:
  755. if contains(c.toBind, s.id):
  756. return symChoice(c.c, n, s, scClosed)
  757. else:
  758. return newIdentNode(s.name, n.info)
  759. of nkPar:
  760. if n.len == 1: return semPatternBody(c, n[0])
  761. else: discard
  762. for i in 0..<n.len:
  763. result[i] = semPatternBody(c, n[i])
  764. proc semPattern(c: PContext, n: PNode; s: PSym): PNode =
  765. openScope(c)
  766. var ctx: TemplCtx
  767. ctx.toBind = initIntSet()
  768. ctx.toMixin = initIntSet()
  769. ctx.toInject = initIntSet()
  770. ctx.c = c
  771. ctx.owner = getCurrOwner(c)
  772. result = flattenStmts(semPatternBody(ctx, n))
  773. if result.kind in {nkStmtList, nkStmtListExpr}:
  774. if result.len == 1:
  775. result = result[0]
  776. elif result.len == 0:
  777. localError(c.config, n.info, "a pattern cannot be empty")
  778. closeScope(c)
  779. addPattern(c, LazySym(sym: s))