semtempl.nim 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 = default(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: var TemplCtx, n: PNode, s: PSym; isField, isAmbiguous: 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.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:
  210. result = symChoice(c.c, n, s, scOpen, isField)
  211. if not isField and result.kind in {nkSym, nkOpenSymChoice}:
  212. if openSym in c.c.features:
  213. if result.kind == nkSym:
  214. result = newOpenSym(result)
  215. else:
  216. result.typ() = nil
  217. else:
  218. result.flags.incl nfDisabledOpenSym
  219. result.typ() = nil
  220. of skGenericParam:
  221. if isField and sfGenSym in s.flags: result = n
  222. else:
  223. result = newSymNodeTypeDesc(s, c.c.idgen, n.info)
  224. if not isField and s.owner != c.owner:
  225. if openSym in c.c.features:
  226. result = newOpenSym(result)
  227. else:
  228. result.flags.incl nfDisabledOpenSym
  229. result.typ() = nil
  230. of skParam:
  231. result = n
  232. of skType:
  233. if isField and sfGenSym in s.flags: result = n
  234. else:
  235. if isAmbiguous:
  236. # ambiguous types should be symchoices since lookup behaves
  237. # differently for them in regular expressions
  238. result = symChoice(c.c, n, s, scOpen, isField)
  239. else: result = newSymNodeTypeDesc(s, c.c.idgen, n.info)
  240. if not isField and not (s.owner == c.owner and
  241. s.typ != nil and s.typ.kind == tyGenericParam) and
  242. result.kind in {nkSym, nkOpenSymChoice}:
  243. if openSym in c.c.features:
  244. if result.kind == nkSym:
  245. result = newOpenSym(result)
  246. else:
  247. result.typ() = nil
  248. else:
  249. result.flags.incl nfDisabledOpenSym
  250. result.typ() = nil
  251. else:
  252. if isField and sfGenSym in s.flags: result = n
  253. else:
  254. result = newSymNode(s, n.info)
  255. if not isField:
  256. if openSym in c.c.features:
  257. result = newOpenSym(result)
  258. else:
  259. result.flags.incl nfDisabledOpenSym
  260. result.typ() = nil
  261. # Issue #12832
  262. when defined(nimsuggest):
  263. suggestSym(c.c.graph, n.info, s, c.c.graph.usageSym, false)
  264. # field access (dot expr) will be handled by builtinFieldAccess
  265. if not isField:
  266. styleCheckUse(c.c, n.info, s)
  267. proc semRoutineInTemplName(c: var TemplCtx, n: PNode, explicitInject: bool): PNode =
  268. result = n
  269. if n.kind == nkIdent:
  270. let s = qualifiedLookUp(c.c, n, {})
  271. if s != nil:
  272. if s.owner == c.owner and (s.kind == skParam or
  273. (sfGenSym in s.flags and not explicitInject)):
  274. incl(s.flags, sfUsed)
  275. result = newSymNode(s, n.info)
  276. onUse(n.info, s)
  277. else:
  278. for i in 0..<n.safeLen:
  279. result[i] = semRoutineInTemplName(c, n[i], explicitInject)
  280. proc semRoutineInTemplBody(c: var TemplCtx, n: PNode, k: TSymKind): PNode =
  281. result = n
  282. checkSonsLen(n, bodyPos + 1, c.c.config)
  283. if n.kind notin nkLambdaKinds:
  284. # routines default to 'inject':
  285. let binding = symBinding(n[pragmasPos])
  286. if binding == spGenSym:
  287. let (ident, hasParam) = getIdentReplaceParams(c, n[namePos])
  288. if not hasParam:
  289. var s = newGenSym(k, ident, c)
  290. s.ast = n
  291. addPrelimDecl(c.c, s)
  292. styleCheckDef(c.c, n.info, s)
  293. onDef(n.info, s)
  294. n[namePos] = newSymNode(s, n[namePos].info)
  295. else:
  296. n[namePos] = ident
  297. else:
  298. n[namePos] = semRoutineInTemplName(c, n[namePos], binding == spInject)
  299. # open scope for parameters
  300. openScope(c)
  301. for i in patternPos..paramsPos-1:
  302. n[i] = semTemplBody(c, n[i])
  303. if k == skTemplate: inc(c.inTemplateHeader)
  304. n[paramsPos] = semTemplBody(c, n[paramsPos])
  305. if k == skTemplate: dec(c.inTemplateHeader)
  306. for i in paramsPos+1..miscPos:
  307. n[i] = semTemplBody(c, n[i])
  308. # open scope for locals
  309. inc c.scopeN
  310. openScope(c)
  311. n[bodyPos] = semTemplBody(c, n[bodyPos])
  312. # close scope for locals
  313. closeScope(c)
  314. dec c.scopeN
  315. # close scope for parameters
  316. closeScope(c)
  317. proc semTemplIdentDef(c: var TemplCtx, a: PNode, symKind: TSymKind) =
  318. checkMinSonsLen(a, 3, c.c.config)
  319. when defined(nimsuggest):
  320. inc c.c.inTypeContext
  321. a[^2] = semTemplBody(c, a[^2])
  322. when defined(nimsuggest):
  323. dec c.c.inTypeContext
  324. a[^1] = semTemplBody(c, a[^1])
  325. for j in 0..<a.len-2:
  326. addLocalDecl(c, a[j], symKind)
  327. proc semTemplSomeDecl(c: var TemplCtx, n: PNode, symKind: TSymKind; start = 0) =
  328. for i in start..<n.len:
  329. var a = n[i]
  330. case a.kind:
  331. of nkCommentStmt: continue
  332. of nkIdentDefs, nkVarTuple, nkConstDef:
  333. semTemplIdentDef(c, a, symKind)
  334. else:
  335. illFormedAst(a, c.c.config)
  336. proc semPattern(c: PContext, n: PNode; s: PSym): PNode
  337. proc semTemplBodySons(c: var TemplCtx, n: PNode): PNode =
  338. result = n
  339. for i in 0..<n.len:
  340. result[i] = semTemplBody(c, n[i])
  341. proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
  342. result = n
  343. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  344. case n.kind
  345. of nkIdent:
  346. if n.ident.id in c.toInject: return n
  347. c.c.isAmbiguous = false
  348. let s = qualifiedLookUp(c.c, n, {})
  349. if s != nil:
  350. if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags:
  351. incl(s.flags, sfUsed)
  352. result = newSymNode(s, n.info)
  353. onUse(n.info, s)
  354. elif contains(c.toBind, s.id):
  355. result = symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  356. elif contains(c.toMixin, s.name.id):
  357. result = symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  358. elif s.owner == c.owner and sfGenSym in s.flags and c.noGenSym == 0:
  359. # template tmp[T](x: var seq[T]) =
  360. # var yz: T
  361. incl(s.flags, sfUsed)
  362. result = newSymNode(s, n.info)
  363. onUse(n.info, s)
  364. else:
  365. if s.kind in {skVar, skLet, skConst}:
  366. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  367. result = semTemplSymbol(c, n, s, c.noGenSym > 0, c.c.isAmbiguous)
  368. of nkBind:
  369. result = semTemplBody(c, n[0])
  370. of nkBindStmt:
  371. result = semBindStmt(c.c, n, c.toBind)
  372. of nkMixinStmt:
  373. if c.scopeN > 0: result = semTemplBodySons(c, n)
  374. else: result = semMixinStmt(c.c, n, c.toMixin)
  375. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  376. discard
  377. of nkIfStmt:
  378. for i in 0..<n.len:
  379. var it = n[i]
  380. if it.len == 2:
  381. openScope(c)
  382. it[0] = semTemplBody(c, it[0])
  383. it[1] = semTemplBody(c, it[1])
  384. closeScope(c)
  385. else:
  386. n[i] = semTemplBodyScope(c, it)
  387. of nkWhileStmt:
  388. openScope(c)
  389. for i in 0..<n.len:
  390. n[i] = semTemplBody(c, n[i])
  391. closeScope(c)
  392. of nkCaseStmt:
  393. openScope(c)
  394. n[0] = semTemplBody(c, n[0])
  395. for i in 1..<n.len:
  396. var a = n[i]
  397. checkMinSonsLen(a, 1, c.c.config)
  398. for j in 0..<a.len-1:
  399. a[j] = semTemplBody(c, a[j])
  400. a[^1] = semTemplBodyScope(c, a[^1])
  401. closeScope(c)
  402. of nkForStmt, nkParForStmt:
  403. openScope(c)
  404. n[^2] = semTemplBody(c, n[^2])
  405. for i in 0..<n.len - 2:
  406. if n[i].kind == nkVarTuple:
  407. for j in 0..<n[i].len-1:
  408. addLocalDecl(c, n[i][j], skForVar)
  409. else:
  410. addLocalDecl(c, n[i], skForVar)
  411. openScope(c)
  412. n[^1] = semTemplBody(c, n[^1])
  413. closeScope(c)
  414. closeScope(c)
  415. of nkBlockStmt, nkBlockExpr, nkBlockType:
  416. checkSonsLen(n, 2, c.c.config)
  417. openScope(c)
  418. if n[0].kind != nkEmpty:
  419. addLocalDecl(c, n[0], skLabel)
  420. when false:
  421. # labels are always 'gensym'ed:
  422. let s = newGenSym(skLabel, n[0], c)
  423. addPrelimDecl(c.c, s)
  424. styleCheckDef(c.c, s)
  425. onDef(n[0].info, s)
  426. n[0] = newSymNode(s, n[0].info)
  427. n[1] = semTemplBody(c, n[1])
  428. closeScope(c)
  429. of nkTryStmt, nkHiddenTryStmt:
  430. checkMinSonsLen(n, 2, c.c.config)
  431. n[0] = semTemplBodyScope(c, n[0])
  432. for i in 1..<n.len:
  433. var a = n[i]
  434. checkMinSonsLen(a, 1, c.c.config)
  435. openScope(c)
  436. for j in 0..<a.len-1:
  437. if a[j].isInfixAs():
  438. addLocalDecl(c, a[j][2], skLet)
  439. a[j][1] = semTemplBody(c, a[j][1])
  440. else:
  441. a[j] = semTemplBody(c, a[j])
  442. a[^1] = semTemplBodyScope(c, a[^1])
  443. closeScope(c)
  444. of nkVarSection: semTemplSomeDecl(c, n, skVar)
  445. of nkLetSection: semTemplSomeDecl(c, n, skLet)
  446. of nkFormalParams:
  447. checkMinSonsLen(n, 1, c.c.config)
  448. semTemplSomeDecl(c, n, skParam, 1)
  449. n[0] = semTemplBody(c, n[0])
  450. of nkConstSection: semTemplSomeDecl(c, n, skConst)
  451. of nkTypeSection:
  452. for i in 0..<n.len:
  453. var a = n[i]
  454. if a.kind == nkCommentStmt: continue
  455. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  456. checkSonsLen(a, 3, c.c.config)
  457. addLocalDecl(c, a[0], skType)
  458. for i in 0..<n.len:
  459. var a = n[i]
  460. if a.kind == nkCommentStmt: continue
  461. if (a.kind != nkTypeDef): illFormedAst(a, c.c.config)
  462. checkSonsLen(a, 3, c.c.config)
  463. if a[1].kind != nkEmpty:
  464. openScope(c)
  465. a[1] = semTemplBody(c, a[1])
  466. a[2] = semTemplBody(c, a[2])
  467. closeScope(c)
  468. else:
  469. a[2] = semTemplBody(c, a[2])
  470. of nkObjectTy:
  471. openScope(c)
  472. result = semTemplBodySons(c, n)
  473. closeScope(c)
  474. of nkRecList:
  475. for i in 0..<n.len:
  476. var a = n[i]
  477. case a.kind:
  478. of nkCommentStmt, nkNilLit, nkSym, nkEmpty: continue
  479. of nkIdentDefs:
  480. semTemplIdentDef(c, a, skField)
  481. of nkRecCase, nkRecWhen:
  482. n[i] = semTemplBody(c, a)
  483. else:
  484. illFormedAst(a, c.c.config)
  485. of nkRecCase:
  486. semTemplIdentDef(c, n[0], skField)
  487. for i in 1..<n.len:
  488. n[i] = semTemplBody(c, n[i])
  489. of nkProcDef, nkLambdaKinds:
  490. result = semRoutineInTemplBody(c, n, skProc)
  491. of nkFuncDef:
  492. result = semRoutineInTemplBody(c, n, skFunc)
  493. of nkMethodDef:
  494. result = semRoutineInTemplBody(c, n, skMethod)
  495. of nkIteratorDef:
  496. result = semRoutineInTemplBody(c, n, skIterator)
  497. of nkTemplateDef:
  498. result = semRoutineInTemplBody(c, n, skTemplate)
  499. of nkMacroDef:
  500. result = semRoutineInTemplBody(c, n, skMacro)
  501. of nkConverterDef:
  502. result = semRoutineInTemplBody(c, n, skConverter)
  503. of nkPragmaExpr:
  504. result = semTemplBodySons(c, n)
  505. of nkPostfix:
  506. result[1] = semTemplBody(c, n[1])
  507. of nkPragma:
  508. for i in 0 ..< n.len:
  509. let x = n[i]
  510. let prag = whichPragma(x)
  511. if prag == wInvalid:
  512. # only sem if not a language-level pragma
  513. result[i] = semTemplBody(c, x)
  514. elif x.kind in nkPragmaCallKinds:
  515. # is pragma, but value still needs to be checked
  516. for j in 1 ..< x.len:
  517. x[j] = semTemplBody(c, x[j])
  518. of nkBracketExpr:
  519. if n.typ == nil:
  520. # if a[b] is nested inside a typed expression, don't convert it
  521. # back to `[]`(a, b), prepareOperand will not typecheck it again
  522. # and so `[]` will not be resolved
  523. # checking if a[b] is typed should be enough to cover this case
  524. result = newNodeI(nkCall, n.info)
  525. result.add newIdentNode(getIdent(c.c.cache, "[]"), n.info)
  526. for i in 0..<n.len: result.add(n[i])
  527. result = semTemplBodySons(c, result)
  528. of nkCurlyExpr:
  529. if n.typ == nil:
  530. # see nkBracketExpr case for explanation
  531. result = newNodeI(nkCall, n.info)
  532. result.add newIdentNode(getIdent(c.c.cache, "{}"), n.info)
  533. for i in 0..<n.len: result.add(n[i])
  534. result = semTemplBodySons(c, result)
  535. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  536. checkSonsLen(n, 2, c.c.config)
  537. let a = n[0]
  538. let b = n[1]
  539. let k = a.kind
  540. case k
  541. of nkBracketExpr:
  542. if a.typ == nil:
  543. # see nkBracketExpr case above for explanation
  544. result = newNodeI(nkCall, n.info)
  545. result.add newIdentNode(getIdent(c.c.cache, "[]="), n.info)
  546. for i in 0..<a.len: result.add(a[i])
  547. result.add(b)
  548. let a0 = semTemplBody(c, a[0])
  549. result = semTemplBodySons(c, result)
  550. of nkCurlyExpr:
  551. if a.typ == nil:
  552. # see nkBracketExpr case above for explanation
  553. result = newNodeI(nkCall, n.info)
  554. result.add newIdentNode(getIdent(c.c.cache, "{}="), n.info)
  555. for i in 0..<a.len: result.add(a[i])
  556. result.add(b)
  557. result = semTemplBodySons(c, result)
  558. else:
  559. result = semTemplBodySons(c, n)
  560. of nkCallKinds-{nkPostfix}:
  561. # do not transform runnableExamples (bug #9143)
  562. if not isRunnableExamples(n[0]):
  563. result = semTemplBodySons(c, n)
  564. of nkDotExpr, nkAccQuoted:
  565. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  566. # so we use the generic code for nkDotExpr too
  567. c.c.isAmbiguous = false
  568. let s = qualifiedLookUp(c.c, n, {})
  569. if s != nil:
  570. # mirror the nkIdent case
  571. # do not symchoice a quoted template parameter (bug #2390):
  572. if s.owner == c.owner and s.kind == skParam and
  573. n.kind == nkAccQuoted and n.len == 1:
  574. incl(s.flags, sfUsed)
  575. onUse(n.info, s)
  576. return newSymNode(s, n.info)
  577. elif contains(c.toBind, s.id):
  578. return symChoice(c.c, n, s, scClosed, c.noGenSym > 0)
  579. elif contains(c.toMixin, s.name.id):
  580. return symChoice(c.c, n, s, scForceOpen, c.noGenSym > 0)
  581. else:
  582. if s.kind in {skVar, skLet, skConst}:
  583. discard qualifiedLookUp(c.c, n, {checkAmbiguity, checkModule})
  584. return semTemplSymbol(c, n, s, c.noGenSym > 0, c.c.isAmbiguous)
  585. if n.kind == nkDotExpr:
  586. result = n
  587. result[0] = semTemplBody(c, n[0])
  588. inc c.noGenSym
  589. result[1] = semTemplBody(c, n[1])
  590. dec c.noGenSym
  591. if result[1].kind == nkSym and result[1].sym.kind in routineKinds:
  592. # prevent `dotTransformation` from rewriting this node to `nkIdent`
  593. # by making it a symchoice
  594. # in generics this becomes `nkClosedSymChoice` but this breaks code
  595. # as the old behavior here was that this became `nkIdent`
  596. var choice = newNodeIT(nkOpenSymChoice, n[1].info, newTypeS(tyNone, c.c))
  597. choice.add result[1]
  598. result[1] = choice
  599. else:
  600. result = semTemplBodySons(c, n)
  601. of nkExprColonExpr, nkExprEqExpr:
  602. if n.len == 2:
  603. inc c.noGenSym
  604. result[0] = semTemplBody(c, n[0])
  605. dec c.noGenSym
  606. result[1] = semTemplBody(c, n[1])
  607. else:
  608. result = semTemplBodySons(c, n)
  609. of nkTableConstr:
  610. # also transform the keys (bug #12595)
  611. for i in 0..<n.len:
  612. result[i] = semTemplBodySons(c, n[i])
  613. else:
  614. result = semTemplBodySons(c, n)
  615. proc semTemplBodyDirty(c: var TemplCtx, n: PNode): PNode =
  616. result = n
  617. semIdeForTemplateOrGenericCheck(c.c.config, n, c.cursorInBody)
  618. case n.kind
  619. of nkIdent:
  620. let s = qualifiedLookUp(c.c, n, {})
  621. if s != nil:
  622. if s.owner == c.owner and s.kind == skParam:
  623. result = newSymNode(s, n.info)
  624. elif contains(c.toBind, s.id):
  625. result = symChoice(c.c, n, s, scClosed)
  626. of nkBind:
  627. result = semTemplBodyDirty(c, n[0])
  628. of nkBindStmt:
  629. result = semBindStmt(c.c, n, c.toBind)
  630. of nkEmpty, nkSym..nkNilLit, nkComesFrom:
  631. discard
  632. else:
  633. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  634. # so we use the generic code for nkDotExpr too
  635. if n.kind == nkDotExpr or n.kind == nkAccQuoted:
  636. let s = qualifiedLookUp(c.c, n, {})
  637. if s != nil and contains(c.toBind, s.id):
  638. return symChoice(c.c, n, s, scClosed)
  639. result = n
  640. for i in 0..<n.len:
  641. result[i] = semTemplBodyDirty(c, n[i])
  642. # in semstmts.nim:
  643. proc semProcAnnotation(c: PContext, prc: PNode; validPragmas: TSpecialWords): PNode
  644. proc semTemplateDef(c: PContext, n: PNode): PNode =
  645. result = semProcAnnotation(c, n, templatePragmas)
  646. if result != nil: return result
  647. result = n
  648. var s: PSym
  649. if isTopLevel(c):
  650. s = semIdentVis(c, skTemplate, n[namePos], {sfExported})
  651. incl(s.flags, sfGlobal)
  652. else:
  653. s = semIdentVis(c, skTemplate, n[namePos], {})
  654. assert s.kind == skTemplate
  655. styleCheckDef(c, s)
  656. onDef(n[namePos].info, s)
  657. # check parameter list:
  658. #s.scope = c.currentScope
  659. # push noalias flag at first to prevent unwanted recursive calls:
  660. incl(s.flags, sfNoalias)
  661. pushOwner(c, s)
  662. openScope(c)
  663. n[namePos] = newSymNode(s)
  664. s.ast = n # for implicitPragmas to use
  665. pragmaCallable(c, s, n, templatePragmas)
  666. implicitPragmas(c, s, n.info, templatePragmas)
  667. setGenericParamsMisc(c, n)
  668. # process parameters:
  669. var allUntyped = true
  670. var nullary = true
  671. if n[paramsPos].kind != nkEmpty:
  672. semParamList(c, n[paramsPos], n[genericParamsPos], s)
  673. # a template's parameters are not gensym'ed even if that was originally the
  674. # case as we determine whether it's a template parameter in the template
  675. # body by the absence of the sfGenSym flag:
  676. let retType = s.typ.returnType
  677. if retType != nil and retType.kind != tyUntyped:
  678. allUntyped = false
  679. for i in 1..<s.typ.n.len:
  680. let param = s.typ.n[i].sym
  681. if param.name.id != ord(wUnderscore):
  682. param.flags.incl sfTemplateParam
  683. param.flags.excl sfGenSym
  684. if param.typ.kind != tyUntyped: allUntyped = false
  685. # no default value, parameters required in call
  686. if param.ast == nil: nullary = false
  687. else:
  688. s.typ = newTypeS(tyProc, c)
  689. # XXX why do we need tyTyped as a return type again?
  690. s.typ.n = newNodeI(nkFormalParams, n.info)
  691. rawAddSon(s.typ, newTypeS(tyTyped, c))
  692. s.typ.n.add newNodeIT(nkType, n.info, s.typ.returnType)
  693. if n[genericParamsPos].safeLen == 0:
  694. # restore original generic type params as no explicit or implicit were found
  695. n[genericParamsPos] = n[miscPos][1]
  696. n[miscPos] = c.graph.emptyNode
  697. if allUntyped: incl(s.flags, sfAllUntyped)
  698. if nullary and
  699. n[genericParamsPos].kind == nkEmpty and
  700. n[bodyPos].kind != nkEmpty:
  701. # template can be called with alias syntax, remove pushed noalias flag
  702. excl(s.flags, sfNoalias)
  703. if n[patternPos].kind != nkEmpty:
  704. n[patternPos] = semPattern(c, n[patternPos], s)
  705. var ctx = TemplCtx(
  706. toBind: initIntSet(),
  707. toMixin: initIntSet(),
  708. toInject: initIntSet(),
  709. c: c,
  710. owner: s
  711. )
  712. # handle default params:
  713. for i in 1..<s.typ.n.len:
  714. let param = s.typ.n[i].sym
  715. if param.ast != nil:
  716. # param default values need to be treated like template body:
  717. if sfDirty in s.flags:
  718. param.ast = semTemplBodyDirty(ctx, param.ast)
  719. else:
  720. param.ast = semTemplBody(ctx, param.ast)
  721. if param.ast.referencesAnotherParam(s):
  722. param.ast.flags.incl nfDefaultRefsParam
  723. if sfDirty in s.flags:
  724. n[bodyPos] = semTemplBodyDirty(ctx, n[bodyPos])
  725. else:
  726. n[bodyPos] = semTemplBody(ctx, n[bodyPos])
  727. # only parameters are resolved, no type checking is performed
  728. semIdeForTemplateOrGeneric(c, n[bodyPos], ctx.cursorInBody)
  729. closeScope(c)
  730. popOwner(c)
  731. if sfCustomPragma in s.flags:
  732. if n[bodyPos].kind != nkEmpty:
  733. localError(c.config, n[bodyPos].info, errImplOfXNotAllowed % s.name.s)
  734. elif n[bodyPos].kind == nkEmpty:
  735. localError(c.config, n.info, "implementation of '$1' expected" % s.name.s)
  736. var (proto, comesFromShadowscope) = searchForProc(c, c.currentScope, s)
  737. if proto == nil:
  738. addInterfaceOverloadableSymAt(c, c.currentScope, s)
  739. elif not comesFromShadowscope:
  740. if {sfTemplateRedefinition, sfGenSym} * s.flags == {}:
  741. #wrongRedefinition(c, n.info, proto.name.s, proto.info)
  742. message(c.config, n.info, warnImplicitTemplateRedefinition, s.name.s)
  743. symTabReplace(c.currentScope.symbols, proto, s)
  744. if n[patternPos].kind != nkEmpty:
  745. c.patterns.add(s)
  746. proc semPatternBody(c: var TemplCtx, n: PNode): PNode =
  747. template templToExpand(s: untyped): untyped =
  748. s.kind == skTemplate and (s.typ.len == 1 or sfAllUntyped in s.flags)
  749. proc newParam(c: var TemplCtx, n: PNode, s: PSym): PNode =
  750. # the param added in the current scope is actually wrong here for
  751. # macros because they have a shadowed param of type 'PNimNode' (see
  752. # semtypes.addParamOrResult). Within the pattern we have to ensure
  753. # to use the param with the proper type though:
  754. incl(s.flags, sfUsed)
  755. onUse(n.info, s)
  756. let x = c.owner.typ.n[s.position+1].sym
  757. assert x.name == s.name
  758. result = newSymNode(x, n.info)
  759. proc handleSym(c: var TemplCtx, n: PNode, s: PSym): PNode =
  760. result = n
  761. if s != nil:
  762. if s.owner == c.owner and s.kind == skParam:
  763. result = newParam(c, n, s)
  764. elif contains(c.toBind, s.id):
  765. result = symChoice(c.c, n, s, scClosed)
  766. elif templToExpand(s):
  767. result = semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  768. else:
  769. discard
  770. # we keep the ident unbound for matching instantiated symbols and
  771. # more flexibility
  772. proc expectParam(c: var TemplCtx, n: PNode): PNode =
  773. let s = qualifiedLookUp(c.c, n, {})
  774. if s != nil and s.owner == c.owner and s.kind == skParam:
  775. result = newParam(c, n, s)
  776. else:
  777. localError(c.c.config, n.info, "invalid expression")
  778. result = n
  779. result = n
  780. case n.kind
  781. of nkIdent:
  782. let s = qualifiedLookUp(c.c, n, {})
  783. result = handleSym(c, n, s)
  784. of nkBindStmt:
  785. result = semBindStmt(c.c, n, c.toBind)
  786. of nkEmpty, nkSym..nkNilLit: discard
  787. of nkCurlyExpr:
  788. # we support '(pattern){x}' to bind a subpattern to a parameter 'x';
  789. # '(pattern){|x}' does the same but the matches will be gathered in 'x'
  790. if n.len != 2:
  791. localError(c.c.config, n.info, "invalid expression")
  792. elif n[1].kind == nkIdent:
  793. n[0] = semPatternBody(c, n[0])
  794. n[1] = expectParam(c, n[1])
  795. elif n[1].kind == nkPrefix and n[1][0].kind == nkIdent:
  796. let opr = n[1][0]
  797. if opr.ident.s == "|":
  798. n[0] = semPatternBody(c, n[0])
  799. n[1][1] = expectParam(c, n[1][1])
  800. else:
  801. localError(c.c.config, n.info, "invalid expression")
  802. else:
  803. localError(c.c.config, n.info, "invalid expression")
  804. of nkStmtList, nkStmtListExpr:
  805. if stupidStmtListExpr(n):
  806. result = semPatternBody(c, n.lastSon)
  807. else:
  808. for i in 0..<n.len:
  809. result[i] = semPatternBody(c, n[i])
  810. of nkCallKinds:
  811. let s = qualifiedLookUp(c.c, n[0], {})
  812. if s != nil:
  813. if s.owner == c.owner and s.kind == skParam: discard
  814. elif contains(c.toBind, s.id): discard
  815. elif templToExpand(s):
  816. return semPatternBody(c, semTemplateExpr(c.c, n, s, {efNoSemCheck}))
  817. if n.kind == nkInfix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  818. # we interpret `*` and `|` only as pattern operators if they occur in
  819. # infix notation, so that '`*`(a, b)' can be used for verbatim matching:
  820. if id.s == "*" or id.s == "**":
  821. result = newNodeI(nkPattern, n.info, n.len)
  822. result[0] = newIdentNode(id, n.info)
  823. result[1] = semPatternBody(c, n[1])
  824. result[2] = expectParam(c, n[2])
  825. return
  826. elif id.s == "|":
  827. result = newNodeI(nkPattern, n.info, n.len)
  828. result[0] = newIdentNode(id, n.info)
  829. result[1] = semPatternBody(c, n[1])
  830. result[2] = semPatternBody(c, n[2])
  831. return
  832. if n.kind == nkPrefix and (let id = considerQuotedIdent(c.c, n[0]); id != nil):
  833. if id.s == "~":
  834. result = newNodeI(nkPattern, n.info, n.len)
  835. result[0] = newIdentNode(id, n.info)
  836. result[1] = semPatternBody(c, n[1])
  837. return
  838. for i in 0..<n.len:
  839. result[i] = semPatternBody(c, n[i])
  840. else:
  841. # dotExpr is ambiguous: note that we explicitly allow 'x.TemplateParam',
  842. # so we use the generic code for nkDotExpr too
  843. case n.kind
  844. of nkDotExpr, nkAccQuoted:
  845. let s = qualifiedLookUp(c.c, n, {})
  846. if s != nil:
  847. if contains(c.toBind, s.id):
  848. return symChoice(c.c, n, s, scClosed)
  849. else:
  850. return newIdentNode(s.name, n.info)
  851. of nkPar:
  852. if n.len == 1: return semPatternBody(c, n[0])
  853. else: discard
  854. for i in 0..<n.len:
  855. result[i] = semPatternBody(c, n[i])
  856. proc semPattern(c: PContext, n: PNode; s: PSym): PNode =
  857. openScope(c)
  858. var ctx = TemplCtx(
  859. toBind: initIntSet(),
  860. toMixin: initIntSet(),
  861. toInject: initIntSet(),
  862. c: c,
  863. owner: getCurrOwner(c)
  864. )
  865. result = flattenStmts(semPatternBody(ctx, n))
  866. if result.kind in {nkStmtList, nkStmtListExpr}:
  867. if result.len == 1:
  868. result = result[0]
  869. elif result.len == 0:
  870. localError(c.config, n.info, "a pattern cannot be empty")
  871. closeScope(c)
  872. addPattern(c, LazySym(sym: s))