semgnrc.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This implements the first pass over the generic body; it resolves some
  10. # symbols. Thus for generics there is a two-phase symbol lookup just like
  11. # in C++.
  12. # A problem is that it cannot be detected if the symbol is introduced
  13. # as in ``var x = ...`` or used because macros/templates can hide this!
  14. # So we have to eval templates/macros right here so that symbol
  15. # lookup can be accurate.
  16. # included from sem.nim
  17. proc getIdentNode(c: PContext; n: PNode): PNode =
  18. case n.kind
  19. of nkPostfix: result = getIdentNode(c, n[1])
  20. of nkPragmaExpr: result = getIdentNode(c, n[0])
  21. of nkIdent, nkAccQuoted, nkSym: result = n
  22. else:
  23. illFormedAst(n, c.config)
  24. result = n
  25. type
  26. GenericCtx = object
  27. toMixin, toBind: IntSet
  28. cursorInBody: bool # only for nimsuggest
  29. bracketExpr: PNode
  30. TSemGenericFlag = enum
  31. withinBind,
  32. withinTypeDesc,
  33. withinMixin,
  34. withinConcept
  35. TSemGenericFlags = set[TSemGenericFlag]
  36. proc semGenericStmt(c: PContext, n: PNode,
  37. flags: TSemGenericFlags, ctx: var GenericCtx): PNode
  38. proc semGenericStmtScope(c: PContext, n: PNode,
  39. flags: TSemGenericFlags,
  40. ctx: var GenericCtx): PNode =
  41. openScope(c)
  42. result = semGenericStmt(c, n, flags, ctx)
  43. closeScope(c)
  44. template macroToExpand(s): untyped =
  45. s.kind in {skMacro, skTemplate} and (s.typ.len == 1 or sfAllUntyped in s.flags)
  46. template macroToExpandSym(s): untyped =
  47. sfCustomPragma notin s.flags and s.kind in {skMacro, skTemplate} and
  48. (s.typ.len == 1) and not fromDotExpr
  49. template isMixedIn(sym): bool =
  50. let s = sym
  51. s.name.id in ctx.toMixin or (withinConcept in flags and
  52. s.magic == mNone and
  53. s.kind in OverloadableSyms)
  54. proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
  55. ctx: var GenericCtx; flags: TSemGenericFlags,
  56. fromDotExpr=false): PNode =
  57. semIdeForTemplateOrGenericCheck(c.config, n, ctx.cursorInBody)
  58. incl(s.flags, sfUsed)
  59. case s.kind
  60. of skUnknown:
  61. # Introduced in this pass! Leave it as an identifier.
  62. result = n
  63. of skProc, skFunc, skMethod, skIterator, skConverter, skModule:
  64. result = symChoice(c, n, s, scOpen)
  65. of skTemplate:
  66. if macroToExpandSym(s):
  67. onUse(n.info, s)
  68. result = semTemplateExpr(c, n, s, {efNoSemCheck})
  69. c.friendModules.add(s.owner.getModule)
  70. result = semGenericStmt(c, result, {}, ctx)
  71. discard c.friendModules.pop()
  72. else:
  73. result = symChoice(c, n, s, scOpen)
  74. of skMacro:
  75. if macroToExpandSym(s):
  76. onUse(n.info, s)
  77. result = semMacroExpr(c, n, n, s, {efNoSemCheck})
  78. c.friendModules.add(s.owner.getModule)
  79. result = semGenericStmt(c, result, {}, ctx)
  80. discard c.friendModules.pop()
  81. else:
  82. result = symChoice(c, n, s, scOpen)
  83. of skGenericParam:
  84. if s.typ != nil and s.typ.kind == tyStatic:
  85. if s.typ.n != nil:
  86. result = s.typ.n
  87. else:
  88. result = n
  89. else:
  90. result = newSymNodeTypeDesc(s, c.idgen, n.info)
  91. onUse(n.info, s)
  92. of skParam:
  93. result = n
  94. onUse(n.info, s)
  95. of skType:
  96. if (s.typ != nil) and
  97. (s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
  98. result = newSymNodeTypeDesc(s, c.idgen, n.info)
  99. else:
  100. result = n
  101. onUse(n.info, s)
  102. of skEnumField:
  103. result = symChoice(c, n, s, scOpen)
  104. else:
  105. result = newSymNode(s, n.info)
  106. onUse(n.info, s)
  107. proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
  108. ctx: var GenericCtx): PNode =
  109. result = n
  110. let ident = considerQuotedIdent(c, n)
  111. var amb = false
  112. var s = searchInScopes(c, ident, amb).skipAlias(n, c.config)
  113. if s == nil:
  114. s = strTableGet(c.pureEnumFields, ident)
  115. #if s != nil and contains(c.ambiguousSymbols, s.id):
  116. # s = nil
  117. if s == nil:
  118. if ident.id notin ctx.toMixin and withinMixin notin flags:
  119. errorUndeclaredIdentifier(c, n.info, ident.s)
  120. else:
  121. if withinBind in flags or s.id in ctx.toBind:
  122. result = symChoice(c, n, s, scClosed)
  123. elif s.isMixedIn:
  124. result = symChoice(c, n, s, scForceOpen)
  125. else:
  126. result = semGenericStmtSymbol(c, n, s, ctx, flags)
  127. # else: leave as nkIdent
  128. proc newDot(n, b: PNode): PNode =
  129. result = newNodeI(nkDotExpr, n.info)
  130. result.add(n[0])
  131. result.add(b)
  132. proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
  133. ctx: var GenericCtx; isMacro: var bool): PNode =
  134. assert n.kind == nkDotExpr
  135. semIdeForTemplateOrGenericCheck(c.config, n, ctx.cursorInBody)
  136. let luf = if withinMixin notin flags: {checkUndeclared, checkModule} else: {checkModule}
  137. var s = qualifiedLookUp(c, n, luf)
  138. if s != nil:
  139. result = semGenericStmtSymbol(c, n, s, ctx, flags)
  140. else:
  141. n[0] = semGenericStmt(c, n[0], flags, ctx)
  142. result = n
  143. let n = n[1]
  144. let ident = considerQuotedIdent(c, n)
  145. var candidates = searchInScopesFilterBy(c, ident, routineKinds) # .skipAlias(n, c.config)
  146. if candidates.len > 0:
  147. let s = candidates[0] # XXX take into account the other candidates!
  148. isMacro = s.kind in {skTemplate, skMacro}
  149. if withinBind in flags or s.id in ctx.toBind:
  150. result = newDot(result, symChoice(c, n, s, scClosed))
  151. elif s.isMixedIn:
  152. result = newDot(result, symChoice(c, n, s, scForceOpen))
  153. else:
  154. let syms = semGenericStmtSymbol(c, n, s, ctx, flags, fromDotExpr=true)
  155. if syms.kind == nkSym:
  156. let choice = symChoice(c, n, s, scForceOpen)
  157. choice.transitionSonsKind(nkClosedSymChoice)
  158. result = newDot(result, choice)
  159. else:
  160. result = newDot(result, syms)
  161. proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
  162. let s = newSymS(skUnknown, getIdentNode(c, n), c)
  163. addPrelimDecl(c, s)
  164. styleCheckDef(c, n.info, s, kind)
  165. onDef(n.info, s)
  166. proc semGenericStmt(c: PContext, n: PNode,
  167. flags: TSemGenericFlags, ctx: var GenericCtx): PNode =
  168. result = n
  169. when defined(nimsuggest):
  170. if withinTypeDesc in flags: inc c.inTypeContext
  171. #if conf.cmd == cmdIdeTools: suggestStmt(c, n)
  172. semIdeForTemplateOrGenericCheck(c.config, n, ctx.cursorInBody)
  173. case n.kind
  174. of nkIdent, nkAccQuoted:
  175. result = lookup(c, n, flags, ctx)
  176. if result != nil and result.kind == nkSym:
  177. assert result.sym != nil
  178. markUsed(c, n.info, result.sym)
  179. of nkDotExpr:
  180. #let luf = if withinMixin notin flags: {checkUndeclared} else: {}
  181. #var s = qualifiedLookUp(c, n, luf)
  182. #if s != nil: result = semGenericStmtSymbol(c, n, s)
  183. # XXX for example: ``result.add`` -- ``add`` needs to be looked up here...
  184. var dummy: bool
  185. result = fuzzyLookup(c, n, flags, ctx, dummy)
  186. of nkSym:
  187. let a = n.sym
  188. let b = getGenSym(c, a)
  189. if b != a: n.sym = b
  190. of nkEmpty, succ(nkSym)..nkNilLit, nkComesFrom:
  191. # see tests/compile/tgensymgeneric.nim:
  192. # We need to open the gensym'ed symbol again so that the instantiation
  193. # creates a fresh copy; but this is wrong the very first reason for gensym
  194. # is that scope rules cannot be used! So simply removing 'sfGenSym' does
  195. # not work. Copying the symbol does not work either because we're already
  196. # the owner of the symbol! What we need to do is to copy the symbol
  197. # in the generic instantiation process...
  198. discard
  199. of nkBind:
  200. result = semGenericStmt(c, n[0], flags+{withinBind}, ctx)
  201. of nkMixinStmt:
  202. result = semMixinStmt(c, n, ctx.toMixin)
  203. of nkBindStmt:
  204. result = semBindStmt(c, n, ctx.toBind)
  205. of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
  206. # check if it is an expression macro:
  207. checkMinSonsLen(n, 1, c.config)
  208. let fn = n[0]
  209. var s = qualifiedLookUp(c, fn, {})
  210. if s == nil and
  211. {withinMixin, withinConcept}*flags == {} and
  212. fn.kind in {nkIdent, nkAccQuoted} and
  213. considerQuotedIdent(c, fn).id notin ctx.toMixin:
  214. errorUndeclaredIdentifier(c, n.info, fn.renderTree)
  215. var first = int ord(withinConcept in flags)
  216. var mixinContext = false
  217. if s != nil:
  218. incl(s.flags, sfUsed)
  219. mixinContext = s.magic in {mDefined, mDeclared, mDeclaredInScope, mCompiles, mAstToStr}
  220. let whichChoice = if s.id in ctx.toBind: scClosed
  221. elif s.isMixedIn: scForceOpen
  222. else: scOpen
  223. let sc = symChoice(c, fn, s, whichChoice)
  224. case s.kind
  225. of skMacro:
  226. if macroToExpand(s) and sc.safeLen <= 1:
  227. onUse(fn.info, s)
  228. result = semMacroExpr(c, n, n, s, {efNoSemCheck})
  229. c.friendModules.add(s.owner.getModule)
  230. result = semGenericStmt(c, result, flags, ctx)
  231. discard c.friendModules.pop()
  232. else:
  233. n[0] = sc
  234. result = n
  235. mixinContext = true
  236. of skTemplate:
  237. if macroToExpand(s) and sc.safeLen <= 1:
  238. onUse(fn.info, s)
  239. result = semTemplateExpr(c, n, s, {efNoSemCheck})
  240. c.friendModules.add(s.owner.getModule)
  241. result = semGenericStmt(c, result, flags, ctx)
  242. discard c.friendModules.pop()
  243. else:
  244. n[0] = sc
  245. result = n
  246. # BUGFIX: we must not return here, we need to do first phase of
  247. # symbol lookup. Also since templates and macros can do scope injections
  248. # we need to put the ``c`` in ``t(c)`` in a mixin context to prevent
  249. # the famous "undeclared identifier: it" bug:
  250. mixinContext = true
  251. of skUnknown, skParam:
  252. # Leave it as an identifier.
  253. discard
  254. of skProc, skFunc, skMethod, skIterator, skConverter, skModule:
  255. result[0] = sc
  256. first = 1
  257. # We're not interested in the example code during this pass so let's
  258. # skip it
  259. if s.magic == mRunnableExamples:
  260. first = result.safeLen # see trunnableexamples.fun3
  261. of skGenericParam:
  262. result[0] = newSymNodeTypeDesc(s, c.idgen, fn.info)
  263. onUse(fn.info, s)
  264. first = 1
  265. of skType:
  266. # bad hack for generics:
  267. if (s.typ != nil) and (s.typ.kind != tyGenericParam):
  268. result[0] = newSymNodeTypeDesc(s, c.idgen, fn.info)
  269. onUse(fn.info, s)
  270. first = 1
  271. else:
  272. result[0] = newSymNode(s, fn.info)
  273. onUse(fn.info, s)
  274. first = 1
  275. elif fn.kind == nkDotExpr:
  276. result[0] = fuzzyLookup(c, fn, flags, ctx, mixinContext)
  277. first = 1
  278. # Consider 'when declared(globalsSlot): ThreadVarSetValue(globalsSlot, ...)'
  279. # in threads.nim: the subtle preprocessing here binds 'globalsSlot' which
  280. # is not exported and yet the generic 'threadProcWrapper' works correctly.
  281. let flags = if mixinContext: flags+{withinMixin} else: flags
  282. for i in first..<result.safeLen:
  283. result[i] = semGenericStmt(c, result[i], flags, ctx)
  284. of nkCurlyExpr:
  285. result = newNodeI(nkCall, n.info)
  286. result.add newIdentNode(getIdent(c.cache, "{}"), n.info)
  287. for i in 0..<n.len: result.add(n[i])
  288. result = semGenericStmt(c, result, flags, ctx)
  289. of nkBracketExpr:
  290. result = newNodeI(nkCall, n.info)
  291. result.add newIdentNode(getIdent(c.cache, "[]"), n.info)
  292. for i in 0..<n.len: result.add(n[i])
  293. result = semGenericStmt(c, result, flags, ctx)
  294. of nkAsgn, nkFastAsgn, nkSinkAsgn:
  295. checkSonsLen(n, 2, c.config)
  296. let a = n[0]
  297. let b = n[1]
  298. let k = a.kind
  299. case k
  300. of nkCurlyExpr:
  301. result = newNodeI(nkCall, n.info)
  302. result.add newIdentNode(getIdent(c.cache, "{}="), n.info)
  303. for i in 0..<a.len: result.add(a[i])
  304. result.add(b)
  305. result = semGenericStmt(c, result, flags, ctx)
  306. of nkBracketExpr:
  307. result = newNodeI(nkCall, n.info)
  308. result.add newIdentNode(getIdent(c.cache, "[]="), n.info)
  309. for i in 0..<a.len: result.add(a[i])
  310. result.add(b)
  311. result = semGenericStmt(c, result, flags, ctx)
  312. else:
  313. for i in 0..<n.len:
  314. result[i] = semGenericStmt(c, n[i], flags, ctx)
  315. of nkIfStmt:
  316. for i in 0..<n.len:
  317. n[i] = semGenericStmtScope(c, n[i], flags, ctx)
  318. of nkWhenStmt:
  319. for i in 0..<n.len:
  320. # bug #8603: conditions of 'when' statements are not
  321. # in a 'mixin' context:
  322. let it = n[i]
  323. if it.kind in {nkElifExpr, nkElifBranch}:
  324. n[i][0] = semGenericStmt(c, it[0], flags, ctx)
  325. n[i][1] = semGenericStmt(c, it[1], flags+{withinMixin}, ctx)
  326. else:
  327. n[i] = semGenericStmt(c, it, flags+{withinMixin}, ctx)
  328. of nkWhileStmt:
  329. openScope(c)
  330. for i in 0..<n.len:
  331. n[i] = semGenericStmt(c, n[i], flags, ctx)
  332. closeScope(c)
  333. of nkCaseStmt:
  334. openScope(c)
  335. n[0] = semGenericStmt(c, n[0], flags, ctx)
  336. for i in 1..<n.len:
  337. var a = n[i]
  338. checkMinSonsLen(a, 1, c.config)
  339. for j in 0..<a.len-1:
  340. a[j] = semGenericStmt(c, a[j], flags, ctx)
  341. a[^1] = semGenericStmtScope(c, a[^1], flags, ctx)
  342. closeScope(c)
  343. of nkForStmt, nkParForStmt:
  344. openScope(c)
  345. n[^2] = semGenericStmt(c, n[^2], flags, ctx)
  346. for i in 0..<n.len - 2:
  347. if (n[i].kind == nkVarTuple):
  348. for s in n[i]:
  349. if (s.kind == nkIdent):
  350. addTempDecl(c,s,skForVar)
  351. else:
  352. addTempDecl(c, n[i], skForVar)
  353. openScope(c)
  354. n[^1] = semGenericStmt(c, n[^1], flags, ctx)
  355. closeScope(c)
  356. closeScope(c)
  357. of nkBlockStmt, nkBlockExpr, nkBlockType:
  358. checkSonsLen(n, 2, c.config)
  359. openScope(c)
  360. if n[0].kind != nkEmpty:
  361. addTempDecl(c, n[0], skLabel)
  362. n[1] = semGenericStmt(c, n[1], flags, ctx)
  363. closeScope(c)
  364. of nkTryStmt, nkHiddenTryStmt:
  365. checkMinSonsLen(n, 2, c.config)
  366. n[0] = semGenericStmtScope(c, n[0], flags, ctx)
  367. for i in 1..<n.len:
  368. var a = n[i]
  369. checkMinSonsLen(a, 1, c.config)
  370. openScope(c)
  371. for j in 0..<a.len-1:
  372. if a[j].isInfixAs():
  373. addTempDecl(c, getIdentNode(c, a[j][2]), skLet)
  374. a[j][1] = semGenericStmt(c, a[j][1], flags+{withinTypeDesc}, ctx)
  375. else:
  376. a[j] = semGenericStmt(c, a[j], flags+{withinTypeDesc}, ctx)
  377. a[^1] = semGenericStmtScope(c, a[^1], flags, ctx)
  378. closeScope(c)
  379. of nkVarSection, nkLetSection, nkConstSection:
  380. let varKind =
  381. case n.kind
  382. of nkVarSection: skVar
  383. of nkLetSection: skLet
  384. else: skConst
  385. for i in 0..<n.len:
  386. var a = n[i]
  387. case a.kind:
  388. of nkCommentStmt: continue
  389. of nkIdentDefs, nkVarTuple, nkConstDef:
  390. checkMinSonsLen(a, 3, c.config)
  391. a[^2] = semGenericStmt(c, a[^2], flags+{withinTypeDesc}, ctx)
  392. a[^1] = semGenericStmt(c, a[^1], flags, ctx)
  393. for j in 0..<a.len-2:
  394. addTempDecl(c, getIdentNode(c, a[j]), varKind)
  395. else:
  396. illFormedAst(a, c.config)
  397. of nkGenericParams:
  398. for i in 0..<n.len:
  399. var a = n[i]
  400. if (a.kind != nkIdentDefs): illFormedAst(a, c.config)
  401. checkMinSonsLen(a, 3, c.config)
  402. a[^2] = semGenericStmt(c, a[^2], flags+{withinTypeDesc}, ctx)
  403. # do not perform symbol lookup for default expressions
  404. for j in 0..<a.len-2:
  405. addTempDecl(c, getIdentNode(c, a[j]), skType)
  406. of nkTypeSection:
  407. for i in 0..<n.len:
  408. var a = n[i]
  409. if a.kind == nkCommentStmt: continue
  410. if (a.kind != nkTypeDef): illFormedAst(a, c.config)
  411. checkSonsLen(a, 3, c.config)
  412. addTempDecl(c, getIdentNode(c, a[0]), skType)
  413. for i in 0..<n.len:
  414. var a = n[i]
  415. if a.kind == nkCommentStmt: continue
  416. if (a.kind != nkTypeDef): illFormedAst(a, c.config)
  417. checkSonsLen(a, 3, c.config)
  418. if a[1].kind != nkEmpty:
  419. openScope(c)
  420. a[1] = semGenericStmt(c, a[1], flags, ctx)
  421. a[2] = semGenericStmt(c, a[2], flags+{withinTypeDesc}, ctx)
  422. closeScope(c)
  423. else:
  424. a[2] = semGenericStmt(c, a[2], flags+{withinTypeDesc}, ctx)
  425. of nkEnumTy:
  426. if n.len > 0:
  427. if n[0].kind != nkEmpty:
  428. n[0] = semGenericStmt(c, n[0], flags+{withinTypeDesc}, ctx)
  429. for i in 1..<n.len:
  430. var a: PNode
  431. case n[i].kind
  432. of nkEnumFieldDef: a = n[i][0]
  433. of nkIdent: a = n[i]
  434. else: illFormedAst(n, c.config)
  435. addDecl(c, newSymS(skUnknown, getIdentNode(c, a), c))
  436. of nkObjectTy, nkTupleTy, nkTupleClassTy:
  437. discard
  438. of nkFormalParams:
  439. checkMinSonsLen(n, 1, c.config)
  440. for i in 1..<n.len:
  441. var a = n[i]
  442. if (a.kind != nkIdentDefs): illFormedAst(a, c.config)
  443. checkMinSonsLen(a, 3, c.config)
  444. a[^2] = semGenericStmt(c, a[^2], flags+{withinTypeDesc}, ctx)
  445. a[^1] = semGenericStmt(c, a[^1], flags, ctx)
  446. for j in 0..<a.len-2:
  447. addTempDecl(c, getIdentNode(c, a[j]), skParam)
  448. # XXX: last change was moving this down here, search for "1.." to keep
  449. # going from this file onward
  450. if n[0].kind != nkEmpty:
  451. n[0] = semGenericStmt(c, n[0], flags+{withinTypeDesc}, ctx)
  452. of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
  453. nkFuncDef, nkIteratorDef, nkLambdaKinds:
  454. checkSonsLen(n, bodyPos + 1, c.config)
  455. if n[namePos].kind != nkEmpty:
  456. addTempDecl(c, getIdentNode(c, n[0]), skProc)
  457. openScope(c)
  458. n[genericParamsPos] = semGenericStmt(c, n[genericParamsPos],
  459. flags, ctx)
  460. if n[paramsPos].kind != nkEmpty:
  461. if n[paramsPos][0].kind != nkEmpty:
  462. addPrelimDecl(c, newSym(skUnknown, getIdent(c.cache, "result"), nextSymId c.idgen, nil, n.info))
  463. n[paramsPos] = semGenericStmt(c, n[paramsPos], flags, ctx)
  464. n[pragmasPos] = semGenericStmt(c, n[pragmasPos], flags, ctx)
  465. var body: PNode
  466. if n[namePos].kind == nkSym:
  467. let s = n[namePos].sym
  468. if sfGenSym in s.flags and s.ast == nil:
  469. body = n[bodyPos]
  470. else:
  471. body = getBody(c.graph, s)
  472. else: body = n[bodyPos]
  473. n[bodyPos] = semGenericStmtScope(c, body, flags, ctx)
  474. closeScope(c)
  475. of nkPragma, nkPragmaExpr: discard
  476. of nkExprColonExpr, nkExprEqExpr:
  477. checkMinSonsLen(n, 2, c.config)
  478. result[1] = semGenericStmt(c, n[1], flags, ctx)
  479. of nkObjConstr:
  480. for i in 0..<n.len:
  481. result[i] = semGenericStmt(c, n[i], flags, ctx)
  482. if result[0].kind == nkSym:
  483. let fmoduleId = getModule(result[0].sym).id
  484. var isVisable = false
  485. for module in c.friendModules:
  486. if module.id == fmoduleId:
  487. isVisable = true
  488. break
  489. if isVisable:
  490. for i in 1..<result.len:
  491. if result[i].kind == nkExprColonExpr:
  492. result[i][1].flags.incl nfSkipFieldChecking
  493. else:
  494. for i in 0..<n.len:
  495. result[i] = semGenericStmt(c, n[i], flags, ctx)
  496. when defined(nimsuggest):
  497. if withinTypeDesc in flags: dec c.inTypeContext
  498. proc semGenericStmt(c: PContext, n: PNode): PNode =
  499. var ctx: GenericCtx
  500. ctx.toMixin = initIntSet()
  501. ctx.toBind = initIntSet()
  502. result = semGenericStmt(c, n, {}, ctx)
  503. semIdeForTemplateOrGeneric(c, result, ctx.cursorInBody)
  504. proc semConceptBody(c: PContext, n: PNode): PNode =
  505. var ctx: GenericCtx
  506. ctx.toMixin = initIntSet()
  507. ctx.toBind = initIntSet()
  508. result = semGenericStmt(c, n, {withinConcept}, ctx)
  509. semIdeForTemplateOrGeneric(c, result, ctx.cursorInBody)