summary refs log tree commit diff stats
path: root/compiler/semgnrc.nim
blob: b8451865eeaeabb7d38abe1b0ca297121febd617 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
discard """
  output: '''abc
def
definition
prefix
xyz
def
definition'''
"""

import critbits

when isMainModule:
  var r: TCritBitTree[void]
  r.incl "abc"
  r.incl "xyz"
  r.incl "def"
  r.incl "definition"
  r.incl "prefix"
  doAssert r.contains"def"
  #r.del "def"

  for w in r.items:
    echo w
    
  for w in r.itemsWithPrefix("de"):
    echo w
n290'>290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This implements the first pass over the generic body; it resolves some
# symbols. Thus for generics there is a two-phase symbol lookup just like
# in C++.
# A problem is that it cannot be detected if the symbol is introduced
# as in ``var x = ...`` or used because macros/templates can hide this!
# So we have to eval templates/macros right here so that symbol
# lookup can be accurate.

# included from sem.nim

proc getIdentNode(n: PNode): PNode =
  case n.kind
  of nkPostfix: result = getIdentNode(n.sons[1])
  of nkPragmaExpr: result = getIdentNode(n.sons[0])
  of nkIdent, nkAccQuoted, nkSym: result = n
  else:
    illFormedAst(n)
    result = n

type
  GenericCtx = object
    toMixin: IntSet
    cursorInBody: bool # only for nimsuggest
    bracketExpr: PNode

type
  TSemGenericFlag = enum
    withinBind, withinTypeDesc, withinMixin
  TSemGenericFlags = set[TSemGenericFlag]

proc semGenericStmt(c: PContext, n: PNode,
                    flags: TSemGenericFlags, ctx: var GenericCtx): PNode

proc semGenericStmtScope(c: PContext, n: PNode,
                         flags: TSemGenericFlags,
                         ctx: var GenericCtx): PNode =
  openScope(c)
  result = semGenericStmt(c, n, flags, ctx)
  closeScope(c)

template macroToExpand(s): untyped =
  s.kind in {skMacro, skTemplate} and (s.typ.len == 1 or sfAllUntyped in s.flags)

template macroToExpandSym(s): untyped =
  s.kind in {skMacro, skTemplate} and (s.typ.len == 1)

proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
                          ctx: var GenericCtx): PNode =
  semIdeForTemplateOrGenericCheck(n, ctx.cursorInBody)
  incl(s.flags, sfUsed)
  case s.kind
  of skUnknown:
    # Introduced in this pass! Leave it as an identifier.
    result = n
  of skProc, skMethod, skIterator, skConverter, skModule:
    result = symChoice(c, n, s, scOpen)
  of skTemplate:
    if macroToExpandSym(s):
      styleCheckUse(n.info, s)
      result = semTemplateExpr(c, n, s, {efNoSemCheck})
      result = semGenericStmt(c, result, {}, ctx)
    else:
      result = symChoice(c, n, s, scOpen)
  of skMacro:
    if macroToExpandSym(s):
      styleCheckUse(n.info, s)
      result = semMacroExpr(c, n, n, s, {efNoSemCheck})
      result = semGenericStmt(c, result, {}, ctx)
    else:
      result = symChoice(c, n, s, scOpen)
  of skGenericParam:
    if s.typ != nil and s.typ.kind == tyStatic:
      if s.typ.n != nil:
        result = s.typ.n
      else:
        result = n
    else:
      result = newSymNodeTypeDesc(s, n.info)
    styleCheckUse(n.info, s)
  of skParam:
    result = n
    styleCheckUse(n.info, s)
  of skType:
    if (s.typ != nil) and
       (s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
      result = newSymNodeTypeDesc(s, n.info)
    else:
      result = n
    styleCheckUse(n.info, s)
  else:
    result = newSymNode(s, n.info)
    styleCheckUse(n.info, s)

proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
            ctx: var GenericCtx): PNode =
  result = n
  let ident = considerQuotedIdent(n)
  var s = searchInScopes(c, ident).skipAlias(n)
  if s == nil:
    if ident.id notin ctx.toMixin and withinMixin notin flags:
      localError(n.info, errUndeclaredIdentifier, ident.s)
  else:
    if withinBind in flags:
      result = symChoice(c, n, s, scClosed)
    elif s.name.id in ctx.toMixin:
      result = symChoice(c, n, s, scForceOpen)
    else:
      result = semGenericStmtSymbol(c, n, s, ctx)
  # else: leave as nkIdent

proc newDot(n, b: PNode): PNode =
  result = newNodeI(nkDotExpr, n.info)
  result.add(n.sons[0])
  result.add(b)

proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
                 ctx: var GenericCtx; isMacro: var bool): PNode =
  assert n.kind == nkDotExpr
  semIdeForTemplateOrGenericCheck(n, ctx.cursorInBody)

  let luf = if withinMixin notin flags: {checkUndeclared, checkModule} else: {checkModule}

  var s = qualifiedLookUp(c, n, luf)
  if s != nil:
    result = semGenericStmtSymbol(c, n, s, ctx)
  else:
    n.sons[0] = semGenericStmt(c, n.sons[0], flags, ctx)
    result = n
    let n = n[1]
    let ident = considerQuotedIdent(n)
    var s = searchInScopes(c, ident).skipAlias(n)
    if s != nil and s.kind in routineKinds:
      isMacro = s.kind in {skTemplate, skMacro}
      if withinBind in flags:
        result = newDot(result, symChoice(c, n, s, scClosed))
      elif s.name.id in ctx.toMixin:
        result = newDot(result, symChoice(c, n, s, scForceOpen))
      else:
        let syms = semGenericStmtSymbol(c, n, s, ctx)
        if syms.kind == nkSym:
          let choice = symChoice(c, n, s, scForceOpen)
          choice.kind = nkClosedSymChoice
          result = newDot(result, choice)
        else:
          result = newDot(result, syms)

proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
  let s = newSymS(skUnknown, getIdentNode(n), c)
  addPrelimDecl(c, s)
  styleCheckDef(n.info, s, kind)

proc semGenericStmt(c: PContext, n: PNode,
                    flags: TSemGenericFlags, ctx: var GenericCtx): PNode =
  result = n
  #if gCmd == cmdIdeTools: suggestStmt(c, n)
  semIdeForTemplateOrGenericCheck(n, ctx.cursorInBody)

  case n.kind
  of nkIdent, nkAccQuoted:
    result = lookup(c, n, flags, ctx)
  of nkDotExpr:
    #let luf = if withinMixin notin flags: {checkUndeclared} else: {}
    #var s = qualifiedLookUp(c, n, luf)
    #if s != nil: result = semGenericStmtSymbol(c, n, s)
    # XXX for example: ``result.add`` -- ``add`` needs to be looked up here...
    var dummy: bool
    result = fuzzyLookup(c, n, flags, ctx, dummy)
  of nkEmpty, nkSym..nkNilLit:
    # see tests/compile/tgensymgeneric.nim:
    # We need to open the gensym'ed symbol again so that the instantiation
    # creates a fresh copy; but this is wrong the very first reason for gensym
    # is that scope rules cannot be used! So simply removing 'sfGenSym' does
    # not work. Copying the symbol does not work either because we're already
    # the owner of the symbol! What we need to do is to copy the symbol
    # in the generic instantiation process...
    discard
  of nkBind:
    result = semGenericStmt(c, n.sons[0], flags+{withinBind}, ctx)
  of nkMixinStmt:
    result = semMixinStmt(c, n, ctx.toMixin)
  of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
    # check if it is an expression macro:
    checkMinSonsLen(n, 1)
    let fn = n.sons[0]
    var s = qualifiedLookUp(c, fn, {})
    if s == nil and withinMixin notin flags and
        fn.kind in {nkIdent, nkAccQuoted} and
        considerQuotedIdent(fn).id notin ctx.toMixin:
      localError(n.info, errUndeclaredIdentifier, fn.renderTree)

    var first = 0
    var mixinContext = false
    if s != nil:
      incl(s.flags, sfUsed)
      mixinContext = s.magic in {mDefined, mDefinedInScope, mCompiles}
      let sc = symChoice(c, fn, s,
            if s.name.id in ctx.toMixin: scForceOpen else: scOpen)
      case s.kind
      of skMacro:
        if macroToExpand(s) and sc.safeLen <= 1:
          styleCheckUse(fn.info, s)
          result = semMacroExpr(c, n, n, s, {efNoSemCheck})
          result = semGenericStmt(c, result, flags, ctx)
        else:
          n.sons[0] = sc
          result = n
        mixinContext = true
      of skTemplate:
        if macroToExpand(s) and sc.safeLen <= 1:
          styleCheckUse(fn.info, s)
          result = semTemplateExpr(c, n, s, {efNoSemCheck})
          result = semGenericStmt(c, result, flags, ctx)
        else:
          n.sons[0] = sc
          result = n
        # BUGFIX: we must not return here, we need to do first phase of
        # symbol lookup. Also since templates and macros can do scope injections
        # we need to put the ``c`` in ``t(c)`` in a mixin context to prevent
        # the famous "undeclared identifier: it" bug:
        mixinContext = true
      of skUnknown, skParam:
        # Leave it as an identifier.
        discard
      of skProc, skMethod, skIterator, skConverter, skModule:
        result.sons[0] = sc
        # do not check of 's.magic==mRoof' here because it might be some
        # other '^' but after overload resolution the proper one:
        if ctx.bracketExpr != nil and n.len == 2 and s.name.s == "^":
          result.add ctx.bracketExpr
        first = 1
      of skGenericParam:
        result.sons[0] = newSymNodeTypeDesc(s, fn.info)
        styleCheckUse(fn.info, s)
        first = 1
      of skType:
        # bad hack for generics:
        if (s.typ != nil) and (s.typ.kind != tyGenericParam):
          result.sons[0] = newSymNodeTypeDesc(s, fn.info)
          styleCheckUse(fn.info, s)
          first = 1
      else:
        result.sons[0] = newSymNode(s, fn.info)
        styleCheckUse(fn.info, s)
        first = 1
    elif fn.kind == nkDotExpr:
      result.sons[0] = fuzzyLookup(c, fn, flags, ctx, mixinContext)
      first = 1
    # Consider 'when declared(globalsSlot): ThreadVarSetValue(globalsSlot, ...)'
    # in threads.nim: the subtle preprocessing here binds 'globalsSlot' which
    # is not exported and yet the generic 'threadProcWrapper' works correctly.
    let flags = if mixinContext: flags+{withinMixin} else: flags
    for i in countup(first, sonsLen(result) - 1):
      result.sons[i] = semGenericStmt(c, result.sons[i], flags, ctx)
  of nkCurlyExpr:
    result = newNodeI(nkCall, n.info)
    result.add newIdentNode(getIdent("{}"), n.info)
    for i in 0 ..< n.len: result.add(n[i])
    result = semGenericStmt(c, result, flags, ctx)
  of nkBracketExpr:
    result = newNodeI(nkCall, n.info)
    result.add newIdentNode(getIdent("[]"), n.info)
    for i in 0 ..< n.len: result.add(n[i])
    withBracketExpr ctx, n.sons[0]:
      result = semGenericStmt(c, result, flags, ctx)
  of nkAsgn, nkFastAsgn:
    checkSonsLen(n, 2)
    let a = n.sons[0]
    let b = n.sons[1]

    let k = a.kind
    case k
    of nkCurlyExpr:
      result = newNodeI(nkCall, n.info)
      result.add newIdentNode(getIdent("{}="), n.info)
      for i in 0 ..< a.len: result.add(a[i])
      result.add(b)
      result = semGenericStmt(c, result, flags, ctx)
    of nkBracketExpr:
      result = newNodeI(nkCall, n.info)
      result.add newIdentNode(getIdent("[]="), n.info)
      for i in 0 ..< a.len: result.add(a[i])
      result.add(b)
      withBracketExpr ctx, a.sons[0]:
        result = semGenericStmt(c, result, flags, ctx)
    else:
      for i in countup(0, sonsLen(n) - 1):
        result.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
  of nkIfStmt:
    for i in countup(0, sonsLen(n)-1):
      n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, ctx)
  of nkWhenStmt:
    for i in countup(0, sonsLen(n)-1):
      n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, ctx)
  of nkWhileStmt:
    openScope(c)
    for i in countup(0, sonsLen(n)-1):
      n.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
    closeScope(c)
  of nkCaseStmt:
    openScope(c)
    n.sons[0] = semGenericStmt(c, n.sons[0], flags, ctx)
    for i in countup(1, sonsLen(n)-1):
      var a = n.sons[i]
      checkMinSonsLen(a, 1)
      var L = sonsLen(a)
      for j in countup(0, L-2):
        a.sons[j] = semGenericStmt(c, a.sons[j], flags, ctx)
      a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
    closeScope(c)
  of nkForStmt, nkParForStmt:
    var L = sonsLen(n)
    openScope(c)
    n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx)
    for i in countup(0, L - 3):
      addTempDecl(c, n.sons[i], skForVar)
    n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx)
    closeScope(c)
  of nkBlockStmt, nkBlockExpr, nkBlockType:
    checkSonsLen(n, 2)
    openScope(c)
    if n.sons[0].kind != nkEmpty:
      addTempDecl(c, n.sons[0], skLabel)
    n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
    closeScope(c)
  of nkTryStmt:
    checkMinSonsLen(n, 2)
    n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, ctx)
    for i in countup(1, sonsLen(n)-1):
      var a = n.sons[i]
      checkMinSonsLen(a, 1)
      var L = sonsLen(a)
      for j in countup(0, L-2):
        a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, ctx)
      a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
  of nkVarSection, nkLetSection:
    for i in countup(0, sonsLen(n) - 1):
      var a = n.sons[i]
      if a.kind == nkCommentStmt: continue
      if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): illFormedAst(a)
      checkMinSonsLen(a, 3)
      var L = sonsLen(a)
      a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
      a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
      for j in countup(0, L-3):
        addTempDecl(c, getIdentNode(a.sons[j]), skVar)
  of nkGenericParams:
    for i in countup(0, sonsLen(n) - 1):
      var a = n.sons[i]
      if (a.kind != nkIdentDefs): illFormedAst(a)
      checkMinSonsLen(a, 3)
      var L = sonsLen(a)
      a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
      # do not perform symbol lookup for default expressions
      for j in countup(0, L-3):
        addTempDecl(c, getIdentNode(a.sons[j]), skType)
  of nkConstSection:
    for i in countup(0, sonsLen(n) - 1):
      var a = n.sons[i]
      if a.kind == nkCommentStmt: continue
      if (a.kind != nkConstDef): illFormedAst(a)
      checkSonsLen(a, 3)
      addTempDecl(c, getIdentNode(a.sons[0]), skConst)
      a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx)
      a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx)
  of nkTypeSection:
    for i in countup(0, sonsLen(n) - 1):
      var a = n.sons[i]
      if a.kind == nkCommentStmt: continue
      if (a.kind != nkTypeDef): illFormedAst(a)
      checkSonsLen(a, 3)
      addTempDecl(c, getIdentNode(a.sons[0]), skType)
    for i in countup(0, sonsLen(n) - 1):
      var a = n.sons[i]
      if a.kind == nkCommentStmt: continue
      if (a.kind != nkTypeDef): illFormedAst(a)
      checkSonsLen(a, 3)
      if a.sons[1].kind != nkEmpty:
        openScope(c)
        a.sons[1] = semGenericStmt(c, a.sons[1], flags, ctx)
        a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
        closeScope(c)
      else:
        a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
  of nkEnumTy:
    if n.sonsLen > 0:
      if n.sons[0].kind != nkEmpty:
        n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
      for i in countup(1, sonsLen(n) - 1):
        var a: PNode
        case n.sons[i].kind
        of nkEnumFieldDef: a = n.sons[i].sons[0]
        of nkIdent: a = n.sons[i]
        else: illFormedAst(n)
        addDecl(c, newSymS(skUnknown, getIdentNode(a), c))
  of nkObjectTy, nkTupleTy, nkTupleClassTy:
    discard
  of nkFormalParams:
    checkMinSonsLen(n, 1)
    if n.sons[0].kind != nkEmpty:
      n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
    for i in countup(1, sonsLen(n) - 1):
      var a = n.sons[i]
      if (a.kind != nkIdentDefs): illFormedAst(a)
      checkMinSonsLen(a, 3)
      var L = sonsLen(a)
      a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
      a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
      for j in countup(0, L-3):
        addTempDecl(c, getIdentNode(a.sons[j]), skParam)
  of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
     nkIteratorDef, nkLambdaKinds:
    checkSonsLen(n, bodyPos + 1)
    if n.sons[namePos].kind != nkEmpty:
      addTempDecl(c, getIdentNode(n.sons[0]), skProc)
    openScope(c)
    n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
                                              flags, ctx)
    if n.sons[paramsPos].kind != nkEmpty:
      if n.sons[paramsPos].sons[0].kind != nkEmpty:
        addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info))
      n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx)
    n.sons[pragmasPos] = semGenericStmt(c, n.sons[pragmasPos], flags, ctx)
    var body: PNode
    if n.sons[namePos].kind == nkSym:
      let s = n.sons[namePos].sym
      if sfGenSym in s.flags and s.ast == nil:
        body = n.sons[bodyPos]
      else:
        body = s.getBody
    else: body = n.sons[bodyPos]
    n.sons[bodyPos] = semGenericStmtScope(c, body, flags, ctx)
    closeScope(c)
  of nkPragma, nkPragmaExpr: discard
  of nkExprColonExpr, nkExprEqExpr:
    checkMinSonsLen(n, 2)
    result.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
  else:
    for i in countup(0, sonsLen(n) - 1):
      result.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)

proc semGenericStmt(c: PContext, n: PNode): PNode =
  var ctx: GenericCtx
  ctx.toMixin = initIntset()
  result = semGenericStmt(c, n, {}, ctx)
  semIdeForTemplateOrGeneric(c, result, ctx.cursorInBody)