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. result = spNone
  29. for i in 0..<n.len:
  30. var it = n[i]
  31. var key = if it.kind == nkExprColonExpr: it[0] else: it
  32. if key.kind == nkIdent:
  33. case whichKeyword(key.ident)
  34. of wGensym: return spGenSym
  35. of wInject: return spInject
  36. else: discard
  37. type
  38. TSymChoiceRule = enum
  39. scClosed, scOpen, scForceOpen
  40. proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule;
  41. isField = false): PNode =
  42. var
  43. a: PSym
  44. o: TOverloadIter
  45. var i = 0
  46. a = initOverloadIter(o, c, n)
  47. while a != nil:
  48. if a.kind != skModule:
  49. inc(i)
  50. if i > 1: break
  51. a = nextOverloadIter(o, c, n)
  52. let info = getCallLineInfo(n)
  53. if i <= 1 and r != scForceOpen:
  54. # XXX this makes more sense but breaks bootstrapping for now:
  55. # (s.kind notin routineKinds or s.magic != mNone):
  56. # for instance 'nextTry' is both in tables.nim and astalgo.nim ...
  57. if not isField or sfGenSym notin s.flags:
  58. result = newSymNode(s, info)
  59. markUsed(c, info, s)
  60. onUse(info, s)
  61. else:
  62. result = n
  63. elif i == 0:
  64. # forced open but symbol not in scope, retain information
  65. result = n
  66. else:
  67. # semantic checking requires a type; ``fitNode`` deals with it
  68. # appropriately
  69. let kind = if r == scClosed or n.kind == nkDotExpr: nkClosedSymChoice
  70. else: nkOpenSymChoice
  71. result = newNodeIT(kind, info, newTypeS(tyNone, c))
  72. a = initOverloadIter(o, c, n)
  73. while a != nil:
  74. if a.kind != skModule and (not isField or sfGenSym notin a.flags):
  75. incl(a.flags, sfUsed)
  76. markOwnerModuleAsUsed(c, a)
  77. result.add newSymNode(a, info)
  78. onUse(info, a)
  79. a = nextOverloadIter(o, c, n)
  80. proc semBindStmt(c: PContext, n: PNode, toBind: var IntSet): PNode =
  81. result = copyNode(n)
  82. for i in 0..<n.len:
  83. var a = n[i]
  84. # If 'a' is an overloaded symbol, we used to use the first symbol
  85. # as a 'witness' and use the fact that subsequent lookups will yield
  86. # the same symbol!
  87. # This is however not true anymore for hygienic templates as semantic
  88. # processing for them changes the symbol table...
  89. let s = qualifiedLookUp(c, a, {checkUndeclared})
  90. if s != nil:
  91. # we need to mark all symbols:
  92. let sc = symChoice(c, n, s, scClosed)
  93. if sc.kind == nkSym:
  94. toBind.incl(sc.sym.id)
  95. result.add sc
  96. else:
  97. for x in items(sc):
  98. toBind.incl(x.sym.id)
  99. result.add x
  100. else:
  101. illFormedAst(a, c.config)
  102. proc semMixinStmt(c: PContext, n: PNode, toMixin: var IntSet): PNode =
  103. result = copyNode(n)
  104. for i in 0..<n.len:
  105. toMixin.incl(considerQuotedIdent(c, n[i]).id)
  106. let x = symChoice(c, n[i], nil, scForceOpen)
  107. result.add x
  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 isTemplParam(c: TemplCtx, s: PSym): bool {.inline.} =
  124. result = s.kind == skParam and
  125. s.owner == c.owner and sfTemplateParam in s.flags
  126. proc getIdentReplaceParams(c: var TemplCtx, n: var PNode): tuple[node: PNode, hasParam: bool] =
  127. case n.kind
  128. of nkPostfix: result = getIdentReplaceParams(c, n[1])
  129. of nkPragmaExpr: result = getIdentReplaceParams(c, n[0])
  130. of nkIdent:
  131. result = (n, false)
  132. let s = qualifiedLookUp(c.c, n, {})
  133. if s != nil and isTemplParam(c, s):
  134. n = newSymNode(s, n.info)
  135. result = (n, true)
  136. of nkSym:
  137. result = (n, isTemplParam(c, n.sym))
  138. of nkAccQuoted:
  139. result = (n, false)
  140. for i in 0..<n.safeLen:
  141. let (ident, hasParam) = getIdentReplaceParams(c, n[i])
  142. if hasParam:
  143. result.node[i] = ident
  144. result.hasParam = true
  145. else:
  146. illFormedAst(n, c.c.config)
  147. result = (n, false)
  148. proc semTemplBody(c: var TemplCtx, n: PNode): PNode
  149. proc openScope(c: var TemplCtx) =
  150. openScope(c.c)
  151. proc closeScope(c: var TemplCtx) =
  152. closeScope(c.c)
  153. proc semTemplBodyScope(c: var TemplCtx, n: PNode): PNode =
  154. openScope(c)
  155. result = semTemplBody(c, n)
  156. closeScope(c)
  157. proc newGenSym(kind: TSymKind, n: PNode, c: var TemplCtx): PSym =
  158. result = newSym(kind, considerQuotedIdent(c.c, n), c.c.idgen, c.owner, n.info)
  159. incl(result.flags, sfGenSym)
  160. incl(result.flags, sfShadowed)
  161. proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) =
  162. # locals default to 'gensym', fields default to 'inject':
  163. if (n.kind == nkPragmaExpr and symBinding(n[1]) == spInject) or
  164. k == skField:
  165. # even if injected, don't produce a sym choice here:
  166. #n = semTemplBody(c, n)
  167. let (ident, hasParam) = getIdentReplaceParams(c, n)
  168. if not hasParam:
  169. if k != skField:
  170. c.toInject.incl(considerQuotedIdent(c.c, ident).id)
  171. else:
  172. if (n.kind == nkPragmaExpr and n.len >= 2 and n[1].kind == nkPragma):
  173. let pragmaNode = n[1]
  174. for i in 0..<pragmaNode.len:
  175. let ni = pragmaNode[i]
  176. # see D20210801T100514
  177. var found = false
  178. if ni.kind == nkIdent:
  179. for a in templatePragmas:
  180. if ni.ident.id == ord(a):
  181. found = true
  182. break
  183. if not found:
  184. openScope(c)
  185. pragmaNode[i] = semTemplBody(c, pragmaNode[i])
  186. closeScope(c)
  187. let (ident, hasParam) = getIdentReplaceParams(c, n)
  188. if not hasParam:
  189. if n.kind != nkSym and not (n.kind == nkIdent and n.ident.id == ord(wUnderscore)):
  190. let local = newGenSym(k, ident, c)
  191. addPrelimDecl(c.c, local)
  192. styleCheckDef(c.c, n.info, local)
  193. onDef(n.info, local)
  194. replaceIdentBySym(c.c, n, newSymNode(local, n.info))
  195. if k == skParam and c.inTemplateHeader > 0:
  196. local.flags.incl sfTemplateParam
  197. proc semTemplSymbol(c: PContext, n: PNode, s: PSym; isField: bool): PNode =
  198. incl(s.flags, sfUsed)
  199. # bug #12885; ideally sem'checking is performed again afterwards marking
  200. # the symbol as used properly, but the nfSem mechanism currently prevents
  201. # that from happening, so we mark the module as used here already:
  202. markOwnerModuleAsUsed(c, s)
  203. # we do not call onUse here, as the identifier is not really
  204. # resolved here. We will fixup the used identifiers later.
  205. case s.kind
  206. of skUnknown:
  207. # Introduced in this pass! Leave it as an identifier.
  208. result = n
  209. of OverloadableSyms-{skTemplate,skMacro}:
  210. result = symChoice(c, n, s, scOpen, isField)
  211. of skTemplate, skMacro:
  212. result = symChoice(c, n, s, scOpen, isField)
  213. if result.kind == nkSym:
  214. # template/macro symbols might need to be semchecked again
  215. # prepareOperand etc don't do this without setting the type to nil
  216. result.typ = nil
  217. of skGenericParam:
  218. if isField and sfGenSym in s.flags: result = n
  219. else: result = newSymNodeTypeDesc(s, c.idgen, n.info)
  220. of skParam:
  221. result = n
  222. of skType:
  223. if isField and sfGenSym in s.flags: result = n
  224. else: result = newSymNodeTypeDesc(s, c.idgen, n.info)
  225. else:
  226. if isField and sfGenSym in s.flags: result = n
  227. else: result = newSymNode(s, n.info)
  228. # Issue #12832
  229. when defined(nimsuggest):
  230. suggestSym(c.graph, n.info, s, c.graph.usageSym, false)
  231. # field access (dot expr) will be handled by builtinFieldAccess
  232. if not isField:
  233. styleCheckUse(c, n.info, s)
  234. proc semRoutineInTemplName(c: var TemplCtx, n: PNode): PNode =
  235. result = n
  236. if n.kind == nkIdent:
  237. let s = qualifiedLookUp(c.c, n, {})
  238. if s != nil:
  239. if s.owner == c.owner and (s.kind == skParam or sfGenSym in s.flags):
  240. incl(s.flags, sfUsed)
  241. result = newSymNode(s, n.info)
  242. onUse(n.info, s)
  243. else:
  244. for i in 0..<n.safeLen:
  245. result[i] = semRoutineInTemplName(c, n[i])
  246. proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
  247. result = n
  248. checkSonsLen(n, bodyPos + 1, c.c.config)
  249. if n.kind notin nkLambdaKinds:
  250. # routines default to 'inject':
  251. if symBinding(n[pragmasPos]) == spGenSym:
  252. let (ident, hasParam) = getIdentReplaceParams(c, n[namePos])
  253. if not hasParam:
  254. var s = newGenSym(k, ident, c)
  255. s.ast = n
  256. addPrelimDecl(c.c, s)
  257. styleCheckDef(c.c, n.info, s)
  258. onDef(n.info, s)
  259. n[namePos] = newSymNode(s, n[namePos].info)
  260. else:
  261. n[namePos] = ident
  262. else:
  263. n[namePos] = semRoutineInTemplName(c, n[namePos])
  264. # open scope for parameters
  265. openScope(c)
  266. for i in patternPos..paramsPos-1:
  267. n[i] = semTemplBody(c, n[i])
  268. if k == skTemplate: inc(c.inTemplateHeader)
  269. n[paramsPos] = semTemplBody(c, n[paramsPos])
  270. if k == skTemplate: dec(c.inTemplateHeader)
  271. for i in paramsPos+1..miscPos:
  272. n[i] = semTemplBody(c, n[i])
  273. # open scope for locals
  274. inc c.scopeN
  275. openScope(c)
  276. n[bodyPos] = semTemplBody(c, n[bodyPos])
  277. # close scope for locals
  278. closeScope(c)
  279. dec c.scopeN
  280. # close scope for parameters
  281. closeScope(c)
  282. proc semTemplIdentDef(c: var TemplCtx, a: PNode, symKind: TSymKind) =
  283. checkMinSonsLen(a, 3, c.c.config)
  284. when defined(nimsuggest):
  285. inc c.c.inTypeContext
  286. a[^2] = semTemplBody(c, a[^2])
  287. when defined(nimsuggest):
  288. dec c.c.inTypeContext
  289. a[^1] = semTemplBody(c, a[^1])
  290. for j in 0..<a.len-2:
  291. addLocalDecl(c, a[j], symKind)
  292. proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start = 0) =
  293. for i in start..<n.len:
  294. var a = n[i]
  295. case a.kind:
  296. of nkCommentStmt: continue
  297. of nkIdentDefs, nkVarTuple, nkConstDef:
  298. semTemplIdentDef(c, a, symKind)
  299. else:
  300. illFormedAst(a, c.c.config)
  301. proc semPattern(c: PContext, n: PNode; s: PSym): PNode
  302. proc semTemplBodySons(c: var TemplCtx, n: PNode): PNode =
  303. result = n
  304. for i in 0..<n.len:
  305. result[i] = semTemplBody(c, n[i])
  306. proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
  307. result = n
  308. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  309. case n.kind
  310. of nkIdent:
  311. if n.ident.id in c.toInject: return n
  312. let s = qualifiedLookUp(c.c, n, {})
  313. if s != nil:
  314. if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags:
  315. incl(s.flags, sfUsed)
  316. result = newSymNode(s, n.info)
  317. onUse(n.info, s)
  318. elif contains(c.toBind, s.id):
  319. result = symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  320. elif contains(c.toMixin, s.name.id):
  321. result = symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  322. elif s.owner == c.owner and sfGenSym in s.flags and c.noGenSym == 0:
  323. # template tmp[T](x: var seq[T]) =
  324. # var yz: T
  325. incl(s.flags, sfUsed)
  326. result = newSymNode(s, n.info)
  327. onUse(n.info, s)
  328. else:
  329. if s.kind in {skType, skVar, skLet, skConst}:
  330. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  331. result = semTemplSymbol(c.c, n, s, c.noGenSym > 0)
  332. of nkBind:
  333. result = semTemplBody(c, n[0])
  334. of nkBindStmt:
  335. result = semBindStmt(c.c, n, c.toBind)
  336. of nkMixinStmt:
  337. if c.scopeN > 0: result = semTemplBodySons(c, n)
  338. else: result = semMixinStmt(c.c, n, c.toMixin)
  339. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  340. discard
  341. of nkIfStmt:
  342. for i in 0..<n.len:
  343. var it = n[i]
  344. if it.len == 2:
  345. openScope(c)
  346. it[0] = semTemplBody(c, it[0])
  347. it[1] = semTemplBody(c, it[1])
  348. closeScope(c)
  349. else:
  350. n[i] = semTemplBodyScope(c, it)
  351. of nkWhileStmt:
  352. openScope(c)
  353. for i in 0..<n.len:
  354. n[i] = semTemplBody(c, n[i])
  355. closeScope(c)
  356. of nkCaseStmt:
  357. openScope(c)
  358. n[0] = semTemplBody(c, n[0])
  359. for i in 1..<n.len:
  360. var a = n[i]
  361. checkMinSonsLen(a, 1, c.c.config)
  362. for j in 0..<a.len-1:
  363. a[j] = semTemplBody(c, a[j])
  364. a[^1] = semTemplBodyScope(c, a[^1])
  365. closeScope(c)
  366. of nkForStmt, nkParForStmt:
  367. openScope(c)
  368. n[^2] = semTemplBody(c, n[^2])
  369. for i in 0..<n.len - 2:
  370. if n[i].kind == nkVarTuple:
  371. for j in 0..<n[i].len-1:
  372. addLocalDecl(c, n[i][j], skForVar)
  373. else:
  374. addLocalDecl(c, n[i], skForVar)
  375. openScope(c)
  376. n[^1] = semTemplBody(c, n[^1])
  377. closeScope(c)
  378. closeScope(c)
  379. of nkBlockStmt, nkBlockExpr, nkBlockType:
  380. checkSonsLen(n, 2, c.c.config)
  381. openScope(c)
  382. if n[0].kind != nkEmpty:
  383. addLocalDecl(c, n[0], skLabel)
  384. when false:
  385. # labels are always 'gensym'ed:
  386. let s = newGenSym(skLabel, n[0], c)
  387. addPrelimDecl(c.c, s)
  388. styleCheckDef(c.c, s)
  389. onDef(n[0].info, s)
  390. n[0] = newSymNode(s, n[0].info)
  391. n[1] = semTemplBody(c, n[1])
  392. closeScope(c)
  393. of nkTryStmt, nkHiddenTryStmt:
  394. checkMinSonsLen(n, 2, c.c.config)
  395. n[0] = semTemplBodyScope(c, n[0])
  396. for i in 1..<n.len:
  397. var a = n[i]
  398. checkMinSonsLen(a, 1, c.c.config)
  399. openScope(c)
  400. for j in 0..<a.len-1:
  401. if a[j].isInfixAs():
  402. addLocalDecl(c, a[j][2], skLet)
  403. a[j][1] = semTemplBody(c, a[j][1])
  404. else:
  405. a[j] = semTemplBody(c, a[j])
  406. a[^1] = semTemplBodyScope(c, a[^1])
  407. closeScope(c)
  408. of nkVarSection: semTemplSomeDecl(c, n, skVar)
  409. of nkLetSection: semTemplSomeDecl(c, n, skLet)
  410. of nkFormalParams:
  411. checkMinSonsLen(n, 1, c.c.config)
  412. semTemplSomeDecl(c, n, skParam, 1)
  413. n[0] = semTemplBody(c, n[0])
  414. of nkConstSection: semTemplSomeDecl(c, n, skConst)
  415. of nkTypeSection:
  416. for i in 0..<n.len:
  417. var a = n[i]
  418. if a.kind == nkCommentStmt: continue
  419. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  420. checkSonsLen(a, 3, c.c.config)
  421. addLocalDecl(c, a[0], skType)
  422. for i in 0..<n.len:
  423. var a = n[i]
  424. if a.kind == nkCommentStmt: continue
  425. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  426. checkSonsLen(a, 3, c.c.config)
  427. if a[1].kind != nkEmpty:
  428. openScope(c)
  429. a[1] = semTemplBody(c, a[1])
  430. a[2] = semTemplBody(c, a[2])
  431. closeScope(c)
  432. else:
  433. a[2] = semTemplBody(c, a[2])
  434. of nkObjectTy:
  435. openScope(c)
  436. result = semTemplBodySons(c, n)
  437. closeScope(c)
  438. of nkRecList:
  439. for i in 0..<n.len:
  440. var a = n[i]
  441. case a.kind:
  442. of nkCommentStmt, nkNilLit, nkSym, nkEmpty: continue
  443. of nkIdentDefs:
  444. semTemplIdentDef(c, a, skField)
  445. of nkRecCase, nkRecWhen:
  446. n[i] = semTemplBody(c, a)
  447. else:
  448. illFormedAst(a, c.c.config)
  449. of nkRecCase:
  450. semTemplIdentDef(c, n[0], skField)
  451. for i in 1..<n.len:
  452. n[i] = semTemplBody(c, n[i])
  453. of nkProcDef, nkLambdaKinds:
  454. result = semRoutineInTemplBody(c, n, skProc)
  455. of nkFuncDef:
  456. result = semRoutineInTemplBody(c, n, skFunc)
  457. of nkMethodDef:
  458. result = semRoutineInTemplBody(c, n, skMethod)
  459. of nkIteratorDef:
  460. result = semRoutineInTemplBody(c, n, skIterator)
  461. of nkTemplateDef:
  462. result = semRoutineInTemplBody(c, n, skTemplate)
  463. of nkMacroDef:
  464. result = semRoutineInTemplBody(c, n, skMacro)
  465. of nkConverterDef:
  466. result = semRoutineInTemplBody(c, n, skConverter)
  467. of nkPragmaExpr:
  468. result[0] = semTemplBody(c, n[0])
  469. of nkPostfix:
  470. result[1] = semTemplBody(c, n[1])
  471. of nkPragma:
  472. for x in n:
  473. if x.kind == nkExprColonExpr:
  474. x[1] = semTemplBody(c, x[1])
  475. of nkBracketExpr:
  476. if n.typ == nil:
  477. # if a[b] is nested inside a typed expression, don't convert it
  478. # back to `[]`(a, b), prepareOperand will not typecheck it again
  479. # and so `[]` will not be resolved
  480. # checking if a[b] is typed should be enough to cover this case
  481. result = newNodeI(nkCall, n.info)
  482. result.add newIdentNode(getIdent(c.c.cache, "[]"), n.info)
  483. for i in 0..<n.len: result.add(n[i])
  484. result = semTemplBodySons(c, result)
  485. of nkCurlyExpr:
  486. if n.typ == nil:
  487. # see nkBracketExpr case for explanation
  488. result = newNodeI(nkCall, n.info)
  489. result.add newIdentNode(getIdent(c.c.cache, "{}"), n.info)
  490. for i in 0..<n.len: result.add(n[i])
  491. result = semTemplBodySons(c, result)
  492. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  493. checkSonsLen(n, 2, c.c.config)
  494. let a = n[0]
  495. let b = n[1]
  496. let k = a.kind
  497. case k
  498. of nkBracketExpr:
  499. if a.typ == nil:
  500. # see nkBracketExpr case above for explanation
  501. result = newNodeI(nkCall, n.info)
  502. result.add newIdentNode(getIdent(c.c.cache, "[]="), n.info)
  503. for i in 0..<a.len: result.add(a[i])
  504. result.add(b)
  505. let a0 = semTemplBody(c, a[0])
  506. result = semTemplBodySons(c, result)
  507. of nkCurlyExpr:
  508. if a.typ == nil:
  509. # see nkBracketExpr case above for explanation
  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. result = semTemplBodySons(c, result)
  515. else:
  516. result = semTemplBodySons(c, n)
  517. of nkCallKinds-{nkPostfix}:
  518. # do not transform runnableExamples (bug #9143)
  519. if not isRunnableExamples(n[0]):
  520. result = semTemplBodySons(c, n)
  521. of nkDotExpr, nkAccQuoted:
  522. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  523. # so we use the generic code for nkDotExpr too
  524. let s = qualifiedLookUp(c.c, n, {})
  525. if s != nil:
  526. # mirror the nkIdent case
  527. # do not symchoice a quoted template parameter (bug #2390):
  528. if s.owner == c.owner and s.kind == skParam and
  529. n.kind == nkAccQuoted and n.len == 1:
  530. incl(s.flags, sfUsed)
  531. onUse(n.info, s)
  532. return newSymNode(s, n.info)
  533. elif contains(c.toBind, s.id):
  534. return symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  535. elif contains(c.toMixin, s.name.id):
  536. return symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  537. else:
  538. if s.kind in {skType, skVar, skLet, skConst}:
  539. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  540. return semTemplSymbol(c.c, n, s, c.noGenSym > 0)
  541. if n.kind == nkDotExpr:
  542. result = n
  543. result[0] = semTemplBody(c, n[0])
  544. inc c.noGenSym
  545. result[1] = semTemplBody(c, n[1])
  546. dec c.noGenSym
  547. if result[1].kind == nkSym and result[1].sym.kind in routineKinds:
  548. # prevent `dotTransformation` from rewriting this node to `nkIdent`
  549. # by making it a symchoice
  550. # in generics this becomes `nkClosedSymChoice` but this breaks code
  551. # as the old behavior here was that this became `nkIdent`
  552. var choice = newNodeIT(nkOpenSymChoice, n[1].info, newTypeS(tyNone, c.c))
  553. choice.add result[1]
  554. result[1] = choice
  555. else:
  556. result = semTemplBodySons(c, n)
  557. of nkExprColonExpr, nkExprEqExpr:
  558. if n.len == 2:
  559. inc c.noGenSym
  560. result[0] = semTemplBody(c, n[0])
  561. dec c.noGenSym
  562. result[1] = semTemplBody(c, n[1])
  563. else:
  564. result = semTemplBodySons(c, n)
  565. of nkTableConstr:
  566. # also transform the keys (bug #12595)
  567. for i in 0..<n.len:
  568. result[i] = semTemplBodySons(c, n[i])
  569. else:
  570. result = semTemplBodySons(c, n)
  571. proc semTemplBodyDirty(c: var TemplCtx, n: PNode): PNode =
  572. result = n
  573. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  574. case n.kind
  575. of nkIdent:
  576. let s = qualifiedLookUp(c.c, n, {})
  577. if s != nil:
  578. if s.owner == c.owner and s.kind == skParam:
  579. result = newSymNode(s, n.info)
  580. elif contains(c.toBind, s.id):
  581. result = symChoice(c.c, n, s, scClosed)
  582. of nkBind:
  583. result = semTemplBodyDirty(c, n[0])
  584. of nkBindStmt:
  585. result = semBindStmt(c.c, n, c.toBind)
  586. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  587. discard
  588. else:
  589. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  590. # so we use the generic code for nkDotExpr too
  591. if n.kind == nkDotExpr or n.kind == nkAccQuoted:
  592. let s = qualifiedLookUp(c.c, n, {})
  593. if s != nil and contains(c.toBind, s.id):
  594. return symChoice(c.c, n, s, scClosed)
  595. result = n
  596. for i in 0..<n.len:
  597. result[i] = semTemplBodyDirty(c, n[i])
  598. # in semstmts.nim:
  599. proc semProcAnnotation(c: PContext, prc: PNode; validPragmas: TSpecialWords): PNode
  600. proc semTemplateDef(c: PContext, n: PNode): PNode =
  601. result = semProcAnnotation(c, n, templatePragmas)
  602. if result != nil: return result
  603. result = n
  604. var s: PSym
  605. if isTopLevel(c):
  606. s = semIdentVis(c, skTemplate, n[namePos], {sfExported})
  607. incl(s.flags, sfGlobal)
  608. else:
  609. s = semIdentVis(c, skTemplate, n[namePos], {})
  610. assert s.kind == skTemplate
  611. styleCheckDef(c, s)
  612. onDef(n[namePos].info, s)
  613. # check parameter list:
  614. #s.scope = c.currentScope
  615. # push noalias flag at first to prevent unwanted recursive calls:
  616. incl(s.flags, sfNoalias)
  617. pushOwner(c, s)
  618. openScope(c)
  619. n[namePos] = newSymNode(s)
  620. pragmaCallable(c, s, n, templatePragmas)
  621. implicitPragmas(c, s, n.info, templatePragmas)
  622. setGenericParamsMisc(c, n)
  623. # process parameters:
  624. var allUntyped = true
  625. var nullary = true
  626. if n[paramsPos].kind != nkEmpty:
  627. semParamList(c, n[paramsPos], n[genericParamsPos], s)
  628. # a template's parameters are not gensym'ed even if that was originally the
  629. # case as we determine whether it's a template parameter in the template
  630. # body by the absence of the sfGenSym flag:
  631. let retType = s.typ.returnType
  632. if retType != nil and retType.kind != tyUntyped:
  633. allUntyped = false
  634. for i in 1..<s.typ.n.len:
  635. let param = s.typ.n[i].sym
  636. if param.name.id != ord(wUnderscore):
  637. param.flags.incl sfTemplateParam
  638. param.flags.excl sfGenSym
  639. if param.typ.kind != tyUntyped: allUntyped = false
  640. # no default value, parameters required in call
  641. if param.ast == nil: nullary = false
  642. else:
  643. s.typ = newTypeS(tyProc, c)
  644. # XXX why do we need tyTyped as a return type again?
  645. s.typ.n = newNodeI(nkFormalParams, n.info)
  646. rawAddSon(s.typ, newTypeS(tyTyped, c))
  647. s.typ.n.add newNodeIT(nkType, n.info, s.typ.returnType)
  648. if n[genericParamsPos].safeLen == 0:
  649. # restore original generic type params as no explicit or implicit were found
  650. n[genericParamsPos] = n[miscPos][1]
  651. n[miscPos] = c.graph.emptyNode
  652. if allUntyped: incl(s.flags, sfAllUntyped)
  653. if nullary and
  654. n[genericParamsPos].kind == nkEmpty and
  655. n[bodyPos].kind != nkEmpty:
  656. # template can be called with alias syntax, remove pushed noalias flag
  657. excl(s.flags, sfNoalias)
  658. if n[patternPos].kind != nkEmpty:
  659. n[patternPos] = semPattern(c, n[patternPos], s)
  660. var ctx = TemplCtx(
  661. toBind: initIntSet(),
  662. toMixin: initIntSet(),
  663. toInject: initIntSet(),
  664. c: c,
  665. owner: s
  666. )
  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. toBind: initIntSet(),
  808. toMixin: initIntSet(),
  809. toInject: initIntSet(),
  810. c: c,
  811. owner: getCurrOwner(c)
  812. )
  813. result = flattenStmts(semPatternBody(ctx, n))
  814. if result.kind in {nkStmtList, nkStmtListExpr}:
  815. if result.len == 1:
  816. result = result[0]
  817. elif result.len == 0:
  818. localError(c.config, n.info, "a pattern cannot be empty")
  819. closeScope(c)
  820. addPattern(c, LazySym(sym: s))