summary refs log tree commit diff stats
path: root/compiler/semstmts.nim
blob: 2e328d730c307a6095a514f133c805b04b80661d (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#
#
#           The Nimrod Compiler
#        (c) Copyright 2011 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## this module does the semantic checking of statements

proc semCommand(c: PContext, n: PNode): PNode =
  result = semExprNoType(c, n)

proc semWhen(c: PContext, n: PNode): PNode = 
  result = nil
  for i in countup(0, sonsLen(n) - 1): 
    var it = n.sons[i]
    case it.kind
    of nkElifBranch: 
      checkSonsLen(it, 2)
      var e = semConstBoolExpr(c, it.sons[0])
      if (e.kind != nkIntLit): InternalError(n.info, "semWhen")
      if (e.intVal != 0) and (result == nil): 
        result = semStmt(c, it.sons[1]) # do not open a new scope!
    of nkElse: 
      checkSonsLen(it, 1)
      if result == nil: 
        result = semStmt(c, it.sons[0]) # do not open a new scope!
    else: illFormedAst(n)
  if result == nil: 
    result = newNodeI(nkNilLit, n.info) 
  # The ``when`` statement implements the mechanism for platform dependant
  # code. Thus we try to ensure here consistent ID allocation after the
  # ``when`` statement.
  IDsynchronizationPoint(200)

proc semIf(c: PContext, n: PNode): PNode = 
  result = n
  for i in countup(0, sonsLen(n) - 1): 
    var it = n.sons[i]
    case it.kind
    of nkElifBranch: 
      checkSonsLen(it, 2)
      openScope(c.tab)
      it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
      it.sons[1] = semStmt(c, it.sons[1])
      closeScope(c.tab)
    of nkElse: 
      if sonsLen(it) == 1: it.sons[0] = semStmtScope(c, it.sons[0])
      else: illFormedAst(it)
    else: illFormedAst(n)
  
proc semDiscard(c: PContext, n: PNode): PNode = 
  result = n
  checkSonsLen(n, 1)
  n.sons[0] = semExprWithType(c, n.sons[0])
  if n.sons[0].typ == nil: localError(n.info, errInvalidDiscard)
  
proc semBreakOrContinue(c: PContext, n: PNode): PNode =
  result = n
  checkSonsLen(n, 1)
  if n.sons[0].kind != nkEmpty: 
    var s: PSym
    case n.sons[0].kind
    of nkIdent: s = lookUp(c, n.sons[0])
    of nkSym: s = n.sons[0].sym
    else: illFormedAst(n)
    if s.kind == skLabel and s.owner.id == c.p.owner.id: 
      var x = newSymNode(s)
      x.info = n.info
      incl(s.flags, sfUsed)
      n.sons[0] = x
    else: 
      localError(n.info, errInvalidControlFlowX, s.name.s)
  elif (c.p.nestedLoopCounter <= 0) and (c.p.nestedBlockCounter <= 0): 
    localError(n.info, errInvalidControlFlowX, 
               renderTree(n, {renderNoComments}))
  
proc semBlock(c: PContext, n: PNode): PNode = 
  result = n
  Inc(c.p.nestedBlockCounter)
  checkSonsLen(n, 2)
  openScope(c.tab)            # BUGFIX: label is in the scope of block!
  if n.sons[0].kind != nkEmpty: 
    var labl = newSymS(skLabel, n.sons[0], c)
    addDecl(c, labl)
    n.sons[0] = newSymNode(labl)
  n.sons[1] = semStmt(c, n.sons[1])
  closeScope(c.tab)
  Dec(c.p.nestedBlockCounter)

proc semAsm(con: PContext, n: PNode): PNode = 
  checkSonsLen(n, 2)
  var marker = pragmaAsm(con, n.sons[0])
  if marker == '\0': marker = '`' # default marker
  result = semAsmOrEmit(con, n, marker)
  
proc semWhile(c: PContext, n: PNode): PNode = 
  result = n
  checkSonsLen(n, 2)
  openScope(c.tab)
  n.sons[0] = forceBool(c, semExprWithType(c, n.sons[0]))
  inc(c.p.nestedLoopCounter)
  n.sons[1] = semStmt(c, n.sons[1])
  dec(c.p.nestedLoopCounter)
  closeScope(c.tab)

proc toCover(t: PType): biggestInt = 
  var t2 = skipTypes(t, abstractVarRange)
  if t2.kind == tyEnum and enumHasHoles(t2): 
    result = sonsLen(t2.n)
  else:
    result = lengthOrd(skipTypes(t, abstractVar))

proc semCase(c: PContext, n: PNode): PNode = 
  # check selector:
  result = n
  checkMinSonsLen(n, 2)
  openScope(c.tab)
  n.sons[0] = semExprWithType(c, n.sons[0])
  var chckCovered = false
  var covered: biggestint = 0
  case skipTypes(n.sons[0].Typ, abstractVarRange).Kind
  of tyInt..tyInt64, tyChar, tyEnum: 
    chckCovered = true
  of tyFloat..tyFloat128, tyString: 
    nil
  else: 
    LocalError(n.info, errSelectorMustBeOfCertainTypes)
    return
  for i in countup(1, sonsLen(n) - 1): 
    var x = n.sons[i]
    case x.kind
    of nkOfBranch: 
      checkMinSonsLen(x, 2)
      semCaseBranch(c, n, x, i, covered)
      var length = sonsLen(x)
      x.sons[length - 1] = semStmtScope(c, x.sons[length - 1])
    of nkElifBranch: 
      chckCovered = false
      checkSonsLen(x, 2)
      x.sons[0] = forceBool(c, semExprWithType(c, x.sons[0]))
      x.sons[1] = semStmtScope(c, x.sons[1])
    of nkElse: 
      chckCovered = false
      checkSonsLen(x, 1)
      x.sons[0] = semStmtScope(c, x.sons[0])
    else: illFormedAst(x)
  if chckCovered and (covered != toCover(n.sons[0].typ)): 
    localError(n.info, errNotAllCasesCovered)
  closeScope(c.tab)

proc SemReturn(c: PContext, n: PNode): PNode = 
  var 
    restype: PType
    a: PNode                  # temporary assignment for code generator
  result = n
  checkSonsLen(n, 1)
  if not (c.p.owner.kind in {skConverter, skMethod, skProc, skMacro}): 
    globalError(n.info, errXNotAllowedHere, "\'return\'")
  if n.sons[0].kind != nkEmpty: 
    n.sons[0] = SemExprWithType(c, n.sons[0]) # check for type compatibility:
    restype = c.p.owner.typ.sons[0]
    if restype != nil: 
      a = newNodeI(nkAsgn, n.sons[0].info)
      n.sons[0] = fitNode(c, restype, n.sons[0])
      # optimize away ``return result``, because it would be transformed
      # to ``result = result; return``:
      if (n.sons[0].kind == nkSym) and (sfResult in n.sons[0].sym.flags): 
        n.sons[0] = ast.emptyNode
      else: 
        if (c.p.resultSym == nil): InternalError(n.info, "semReturn")
        addSon(a, semExprWithType(c, newSymNode(c.p.resultSym)))
        addSon(a, n.sons[0])
        n.sons[0] = a
    else: 
      localError(n.info, errCannotReturnExpr)
  
proc SemYield(c: PContext, n: PNode): PNode = 
  result = n
  checkSonsLen(n, 1)
  if (c.p.owner == nil) or (c.p.owner.kind != skIterator): 
    GlobalError(n.info, errYieldNotAllowedHere)
  if n.sons[0].kind != nkEmpty: 
    n.sons[0] = SemExprWithType(c, n.sons[0]) # check for type compatibility:
    var restype = c.p.owner.typ.sons[0]
    if restype != nil: 
      n.sons[0] = fitNode(c, restype, n.sons[0])
      if (n.sons[0].typ == nil): InternalError(n.info, "semYield")
    else: 
      localError(n.info, errCannotReturnExpr)
  
proc fitRemoveHiddenConv(c: PContext, typ: Ptype, n: PNode): PNode = 
  result = fitNode(c, typ, n)
  if result.kind in {nkHiddenStdConv, nkHiddenSubConv}: 
    changeType(result.sons[1], typ)
    result = result.sons[1]
  elif not sameType(result.typ, typ): 
    changeType(result, typ)
  
proc semIdentDef(c: PContext, n: PNode, kind: TSymKind): PSym =
  if isTopLevel(c): 
    result = semIdentWithPragma(c, kind, n, {sfStar, sfMinus})
    incl(result.flags, sfGlobal)
  else: 
    result = semIdentWithPragma(c, kind, n, {})
  
proc semVar(c: PContext, n: PNode): PNode = 
  var b: PNode
  result = copyNode(n)
  for i in countup(0, sonsLen(n)-1): 
    var a = n.sons[i]
    if gCmd == cmdIdeTools: suggestStmt(c, a)
    if a.kind == nkCommentStmt: continue 
    if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): IllFormedAst(a)
    checkMinSonsLen(a, 3)
    var length = sonsLen(a)
    var typ: PType
    if a.sons[length-2].kind != nkEmpty:
      typ = semTypeNode(c, a.sons[length-2], nil)
    else: 
      typ = nil
    var def: PNode
    if a.sons[length-1].kind != nkEmpty: 
      def = semExprWithType(c, a.sons[length-1])
      # BUGFIX: ``fitNode`` is needed here!
      # check type compability between def.typ and typ:
      if typ != nil: def = fitNode(c, typ, def)
      else: typ = def.typ
    else: 
      def = ast.emptyNode
    # this can only happen for errornous var statements:
    if typ == nil: continue
    if not typeAllowed(typ, skVar): 
      GlobalError(a.info, errXisNoType, typeToString(typ))
    var tup = skipTypes(typ, {tyGenericInst})
    if a.kind == nkVarTuple: 
      if tup.kind != tyTuple: GlobalError(a.info, errXExpected, "tuple")
      if length - 2 != sonsLen(tup): 
        GlobalError(a.info, errWrongNumberOfVariables)
      b = newNodeI(nkVarTuple, a.info)
      newSons(b, length)
      b.sons[length - 2] = ast.emptyNode # no type desc
      b.sons[length - 1] = def
      addSon(result, b)
    for j in countup(0, length-3): 
      var v = semIdentDef(c, a.sons[j], skVar)
      if v.flags * {sfStar, sfMinus} != {}: incl(v.flags, sfInInterface)
      addInterfaceDecl(c, v)
      if a.kind != nkVarTuple: 
        v.typ = typ
        b = newNodeI(nkIdentDefs, a.info)
        addSon(b, newSymNode(v))
        addSon(b, ast.emptyNode)        # no type description
        addSon(b, copyTree(def))
        addSon(result, b)
      else: 
        v.typ = tup.sons[j]
        b.sons[j] = newSymNode(v)

proc semConst(c: PContext, n: PNode): PNode = 
  result = copyNode(n)
  for i in countup(0, sonsLen(n) - 1): 
    var a = n.sons[i]
    if gCmd == cmdIdeTools: suggestStmt(c, a)
    if a.kind == nkCommentStmt: continue 
    if (a.kind != nkConstDef): IllFormedAst(a)
    checkSonsLen(a, 3)
    var v = semIdentDef(c, a.sons[0], skConst)
    var typ: PType = nil
    if a.sons[1].kind != nkEmpty: typ = semTypeNode(c, a.sons[1], nil)

    var e = semExprWithType(c, a.sons[2])
    if e == nil: GlobalError(a.sons[2].info, errConstExprExpected)
    var def = getConstExpr(c.module, e)
    if def == nil: 
      v.flags.incl(sfFakeConst)
      def = evalConstExpr(c.module, e)
      if def == nil or def.kind == nkEmpty: def = e
    # check type compatibility between def.typ and typ:
    if typ != nil:
      def = fitRemoveHiddenConv(c, typ, def)
    else:
      typ = def.typ
    if not typeAllowed(typ, skConst):
      v.flags.incl(sfFakeConst)
      if not typeAllowed(typ, skVar):
        GlobalError(a.info, errXisNoType, typeToString(typ))
    v.typ = typ
    v.ast = def               # no need to copy
    if v.flags * {sfStar, sfMinus} != {}: incl(v.flags, sfInInterface)
    addInterfaceDecl(c, v)
    var b = newNodeI(nkConstDef, a.info)
    addSon(b, newSymNode(v))
    addSon(b, ast.emptyNode)            # no type description
    addSon(b, copyTree(def))
    addSon(result, b)

proc transfFieldLoopBody(n: PNode, forLoop: PNode,
                         tupleType: PType,
                         tupleIndex, first: int): PNode = 
  case n.kind
  of nkEmpty..pred(nkIdent), succ(nkIdent)..nkNilLit: result = n
  of nkIdent:
    result = n
    var L = sonsLen(forLoop)
    # field name:
    if first > 0:
      if n.ident.id == forLoop[0].ident.id:
        if tupleType.n == nil: 
          # ugh, there are no field names:
          result = newStrNode(nkStrLit, "")
        else:
          result = newStrNode(nkStrLit, tupleType.n.sons[tupleIndex].sym.name.s)
        return
    # other fields:
    for i in first..L-3:
      if n.ident.id == forLoop[i].ident.id:
        var call = forLoop.sons[L-2]
        var tupl = call.sons[i+1-first]
        result = newNodeI(nkBracketExpr, n.info)
        result.add(tupl)
        result.add(newIntNode(nkIntLit, tupleIndex))
        break
  else:
    result = copyNode(n)
    newSons(result, sonsLen(n))
    for i in countup(0, sonsLen(n)-1):
      result.sons[i] = transfFieldLoopBody(n.sons[i], forLoop,
                                           tupleType, tupleIndex, first)

proc semForFields(c: PContext, n: PNode, m: TMagic): PNode = 
  # so that 'break' etc. work as expected, we produce 
  # a 'while true: stmt; break' loop ...
  result = newNodeI(nkWhileStmt, n.info)
  var trueSymbol = StrTableGet(magicsys.systemModule.Tab, getIdent"true")
  if trueSymbol == nil: GlobalError(n.info, errSystemNeeds, "true")

  result.add(newSymNode(trueSymbol, n.info))
  var stmts = newNodeI(nkStmtList, n.info)
  result.add(stmts)
  
  var length = sonsLen(n)
  var call = n.sons[length-2]
  if length-2 != sonsLen(call)-1 + ord(m==mFieldPairs):
    GlobalError(n.info, errWrongNumberOfVariables)
  
  var tupleTypeA = skipTypes(call.sons[1].typ, abstractVar)
  if tupleTypeA.kind != tyTuple: InternalError(n.info, "no tuple type!")
  for i in 1..call.len-1:
    var tupleTypeB = skipTypes(call.sons[i].typ, abstractVar)
    if not SameType(tupleTypeA, tupleTypeB):
      typeMismatch(call.sons[i], tupleTypeA, tupleTypeB)
  
  Inc(c.p.nestedLoopCounter)
  var loopBody = n.sons[length-1]
  for i in 0..sonsLen(tupleTypeA)-1:
    openScope(c.tab)
    var body = transfFieldLoopBody(loopBody, n, tupleTypeA, i,
                                   ord(m==mFieldPairs))
    stmts.add(SemStmt(c, body))
    closeScope(c.tab)
  Dec(c.p.nestedLoopCounter)
  var b = newNodeI(nkBreakStmt, n.info)
  b.add(ast.emptyNode)
  stmts.add(b)

proc semFor(c: PContext, n: PNode): PNode = 
  result = n
  checkMinSonsLen(n, 3)
  var length = sonsLen(n)
  openScope(c.tab)
  n.sons[length-2] = semExprWithType(c, n.sons[length-2], {efWantIterator})
  var call = n.sons[length-2]
  if call.kind != nkCall or call.sons[0].kind != nkSym or
      call.sons[0].sym.kind != skIterator: 
    GlobalError(n.sons[length - 2].info, errIteratorExpected)
  elif call.sons[0].sym.magic != mNone:
    result = semForFields(c, n, call.sons[0].sym.magic)
  else:
    var iter = skipTypes(n.sons[length-2].typ, {tyGenericInst})
    # length == 3 means that there is one for loop variable
    # and thus no tuple unpacking:
    if iter.kind != tyTuple or length == 3: 
      if length != 3: GlobalError(n.info, errWrongNumberOfVariables)
      var v = newSymS(skForVar, n.sons[0], c)
      v.typ = iter
      n.sons[0] = newSymNode(v)
      addDecl(c, v)
    else: 
      if length-2 != sonsLen(iter):
        GlobalError(n.info, errWrongNumberOfVariables)
      for i in countup(0, length - 3): 
        var v = newSymS(skForVar, n.sons[i], c)
        v.typ = iter.sons[i]
        n.sons[i] = newSymNode(v)
        addDecl(c, v)
    Inc(c.p.nestedLoopCounter)
    n.sons[length-1] = SemStmt(c, n.sons[length-1])
    Dec(c.p.nestedLoopCounter)
  closeScope(c.tab)

proc semRaise(c: PContext, n: PNode): PNode = 
  result = n
  checkSonsLen(n, 1)
  if n.sons[0].kind != nkEmpty: 
    n.sons[0] = semExprWithType(c, n.sons[0])
    var typ = n.sons[0].typ
    if (typ.kind != tyRef) or (typ.sons[0].kind != tyObject): 
      localError(n.info, errExprCannotBeRaised)
  
proc semTry(c: PContext, n: PNode): PNode = 
  result = n
  checkMinSonsLen(n, 2)
  n.sons[0] = semStmtScope(c, n.sons[0])
  var check = initIntSet()
  for i in countup(1, sonsLen(n) - 1): 
    var a = n.sons[i]
    checkMinSonsLen(a, 1)
    var length = sonsLen(a)
    if a.kind == nkExceptBranch: 
      for j in countup(0, length - 2): 
        var typ = semTypeNode(c, a.sons[j], nil)
        if typ.kind == tyRef: typ = typ.sons[0]
        if typ.kind != tyObject: 
          GlobalError(a.sons[j].info, errExprCannotBeRaised)
        a.sons[j] = newNodeI(nkType, a.sons[j].info)
        a.sons[j].typ = typ
        if ContainsOrIncl(check, typ.id): 
          localError(a.sons[j].info, errExceptionAlreadyHandled)
    elif a.kind != nkFinally: 
      illFormedAst(n) 
    # last child of an nkExcept/nkFinally branch is a statement:
    a.sons[length - 1] = semStmtScope(c, a.sons[length - 1])

proc addGenericParamListToScope(c: PContext, n: PNode) = 
  if n.kind != nkGenericParams: 
    InternalError(n.info, "addGenericParamListToScope")
  for i in countup(0, sonsLen(n)-1): 
    var a = n.sons[i]
    if a.kind != nkSym: internalError(a.info, "addGenericParamListToScope")
    addDecl(c, a.sym)

proc typeSectionLeftSidePass(c: PContext, n: PNode) = 
  # process the symbols on the left side for the whole type section, before
  # we even look at the type definitions on the right
  for i in countup(0, sonsLen(n) - 1): 
    var a = n.sons[i]
    if gCmd == cmdIdeTools: suggestStmt(c, a)
    if a.kind == nkCommentStmt: continue 
    if a.kind != nkTypeDef: IllFormedAst(a)
    checkSonsLen(a, 3)
    var s = semIdentDef(c, a.sons[0], skType)
    if s.flags * {sfStar, sfMinus} != {}: incl(s.flags, sfInInterface)
    s.typ = newTypeS(tyForward, c)
    s.typ.sym = s             # process pragmas:
    if a.sons[0].kind == nkPragmaExpr:
      pragma(c, s, a.sons[0].sons[1], typePragmas) 
    # add it here, so that recursive types are possible:
    addInterfaceDecl(c, s)
    a.sons[0] = newSymNode(s)

proc typeSectionRightSidePass(c: PContext, n: PNode) =
  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[0].kind != nkSym): IllFormedAst(a)
    var s = a.sons[0].sym
    if s.magic == mNone and a.sons[2].kind == nkEmpty: 
      GlobalError(a.info, errImplOfXexpected, s.name.s)
    if s.magic != mNone: processMagicType(c, s)
    if a.sons[1].kind != nkEmpty: 
      # We have a generic type declaration here. In generic types,
      # symbol lookup needs to be done here.
      openScope(c.tab)
      pushOwner(s)
      s.typ.kind = tyGenericBody
      if s.typ.containerID != 0: 
        InternalError(a.info, "semTypeSection: containerID")
      s.typ.containerID = getID()
      a.sons[1] = semGenericParamList(c, a.sons[1], s.typ)
      # we fill it out later. For magic generics like 'seq', it won't be filled
      # so we use tyEmpty instead of nil to not crash for strange conversions
      # like: mydata.seq
      addSon(s.typ, newTypeS(tyEmpty, c))
      s.ast = a
      var body = semTypeNode(c, a.sons[2], nil)
      if body != nil: body.sym = s
      s.typ.sons[sonsLen(s.typ) - 1] = body
      popOwner()
      closeScope(c.tab)
    elif a.sons[2].kind != nkEmpty: 
      # process the type's body:
      pushOwner(s)
      var t = semTypeNode(c, a.sons[2], s.typ)
      if s.typ == nil: 
        s.typ = t
      elif t != s.typ: 
        # this can happen for e.g. tcan_alias_specialised_generic:
        assignType(s.typ, t)
        #debug s.typ
      s.ast = a
      popOwner()

proc typeSectionFinalPass(c: PContext, n: PNode) = 
  for i in countup(0, sonsLen(n) - 1): 
    var a = n.sons[i]
    if a.kind == nkCommentStmt: continue 
    if (a.sons[0].kind != nkSym): IllFormedAst(a)
    var s = a.sons[0].sym         
    # compute the type's size and check for illegal recursions:
    if a.sons[1].kind == nkEmpty: 
      if a.sons[2].kind in {nkSym, nkIdent, nkAccQuoted}:
        # type aliases are hard:
        #MessageOut('for type ' + typeToString(s.typ));
        var t = semTypeNode(c, a.sons[2], nil)
        if t.kind in {tyObject, tyEnum}: 
          assignType(s.typ, t)
          s.typ.id = t.id     # same id
      checkConstructedType(s.info, s.typ)

proc SemTypeSection(c: PContext, n: PNode): PNode =
  typeSectionLeftSidePass(c, n)
  typeSectionRightSidePass(c, n)
  typeSectionFinalPass(c, n)
  result = n

proc semParamList(c: PContext, n, genericParams: PNode, s: PSym) = 
  s.typ = semProcTypeNode(c, n, genericParams, nil)

proc addParams(c: PContext, n: PNode) = 
  for i in countup(1, sonsLen(n)-1): 
    if (n.sons[i].kind != nkSym): InternalError(n.info, "addParams")
    addDecl(c, n.sons[i].sym)

proc semBorrow(c: PContext, n: PNode, s: PSym) = 
  # search for the correct alias:
  var b = SearchForBorrowProc(c, s, c.tab.tos - 2)
  if b != nil: 
    # store the alias:
    n.sons[codePos] = newSymNode(b)
  else:
    LocalError(n.info, errNoSymbolToBorrowFromFound) 

proc sideEffectsCheck(c: PContext, s: PSym) = 
  if {sfNoSideEffect, sfSideEffect} * s.flags ==
      {sfNoSideEffect, sfSideEffect}: 
    LocalError(s.info, errXhasSideEffects, s.name.s)
  
proc addResult(c: PContext, t: PType, info: TLineInfo) = 
  if t != nil: 
    var s = newSym(skVar, getIdent("result"), getCurrOwner())
    s.info = info
    s.typ = t
    incl(s.flags, sfResult)
    incl(s.flags, sfUsed)
    addDecl(c, s)
    c.p.resultSym = s

proc addResultNode(c: PContext, n: PNode) = 
  if c.p.resultSym != nil: addSon(n, newSymNode(c.p.resultSym))
  
proc semLambda(c: PContext, n: PNode): PNode = 
  result = n
  checkSonsLen(n, codePos + 1)
  var s = newSym(skProc, getIdent(":anonymous"), getCurrOwner())
  s.info = n.info
  s.ast = n
  n.sons[namePos] = newSymNode(s)
  pushOwner(s)
  openScope(c.tab)
  if (n.sons[genericParamsPos].kind != nkEmpty): 
    illFormedAst(n)           # process parameters:
  if n.sons[paramsPos].kind != nkEmpty: 
    semParamList(c, n.sons[ParamsPos], nil, s)
    addParams(c, s.typ.n)
  else: 
    s.typ = newTypeS(tyProc, c)
    addSon(s.typ, nil)
  s.typ.callConv = ccClosure
  if n.sons[pragmasPos].kind != nkEmpty:
    pragma(c, s, n.sons[pragmasPos], lambdaPragmas)
  s.options = gOptions
  if n.sons[codePos].kind != nkEmpty: 
    if sfImportc in s.flags: 
      LocalError(n.sons[codePos].info, errImplOfXNotAllowed, s.name.s)
    pushProcCon(c, s)
    addResult(c, s.typ.sons[0], n.info)
    n.sons[codePos] = semStmtScope(c, n.sons[codePos])
    addResultNode(c, n)
    popProcCon(c)
  else: 
    LocalError(n.info, errImplOfXexpected, s.name.s)
  closeScope(c.tab)           # close scope for parameters
  popOwner()
  result.typ = s.typ

proc semProcAux(c: PContext, n: PNode, kind: TSymKind, 
                validPragmas: TSpecialWords): PNode = 
  var 
    proto: PSym
    gp: PNode
  result = n
  checkSonsLen(n, codePos + 1)
  var s = semIdentDef(c, n.sons[0], kind)
  n.sons[namePos] = newSymNode(s)
  if sfStar in s.flags: incl(s.flags, sfInInterface)
  s.ast = n
  pushOwner(s)
  openScope(c.tab)
  if n.sons[genericParamsPos].kind != nkEmpty: 
    n.sons[genericParamsPos] = semGenericParamList(c, n.sons[genericParamsPos])
    gp = n.sons[genericParamsPos]
  else: 
    gp = newNodeI(nkGenericParams, n.info)
  # process parameters:
  if n.sons[paramsPos].kind != nkEmpty:
    semParamList(c, n.sons[ParamsPos], gp, s)
    if sonsLen(gp) > 0: 
      if n.sons[genericParamsPos].kind == nkEmpty:
        # we have a list of implicit type parameters:
        n.sons[genericParamsPos] = gp
        # check for semantics again:
        semParamList(c, n.sons[ParamsPos], nil, s)
    addParams(c, s.typ.n)
  else: 
    s.typ = newTypeS(tyProc, c)
    addSon(s.typ, nil)
  proto = SearchForProc(c, s, c.tab.tos - 2) # -2 because we have a scope open
                                             # for parameters
  if proto == nil: 
    if c.p.owner.kind != skModule: 
      s.typ.callConv = ccClosure
    else: 
      s.typ.callConv = lastOptionEntry(c).defaultCC 
    # add it here, so that recursive procs are possible:
    # -2 because we have a scope open for parameters
    if kind in OverloadableSyms: 
      addInterfaceOverloadableSymAt(c, s, c.tab.tos - 2)
    else: 
      addInterfaceDeclAt(c, s, c.tab.tos - 2)
    if n.sons[pragmasPos].kind != nkEmpty:
      pragma(c, s, n.sons[pragmasPos], validPragmas)
  else: 
    if n.sons[pragmasPos].kind != nkEmpty: 
      LocalError(n.sons[pragmasPos].info, errPragmaOnlyInHeaderOfProc)
    if sfForward notin proto.flags: 
      LocalError(n.info, errAttemptToRedefine, proto.name.s)
    excl(proto.flags, sfForward)
    closeScope(c.tab)         # close scope with wrong parameter symbols
    openScope(c.tab)          # open scope for old (correct) parameter symbols
    if proto.ast.sons[genericParamsPos].kind != nkEmpty: 
      addGenericParamListToScope(c, proto.ast.sons[genericParamsPos])
    addParams(c, proto.typ.n)
    proto.info = s.info       # more accurate line information
    s.typ = proto.typ
    s = proto
    n.sons[genericParamsPos] = proto.ast.sons[genericParamsPos]
    n.sons[paramsPos] = proto.ast.sons[paramsPos]
    if (n.sons[namePos].kind != nkSym): InternalError(n.info, "semProcAux")
    n.sons[namePos].sym = proto
    proto.ast = n             # needed for code generation
    popOwner()
    pushOwner(s)
  s.options = gOptions
  if n.sons[codePos].kind != nkEmpty: 
    # for DLL generation it is annoying to check for sfImportc!
    if sfBorrow in s.flags: 
      LocalError(n.sons[codePos].info, errImplOfXNotAllowed, s.name.s)
    if n.sons[genericParamsPos].kind == nkEmpty: 
      pushProcCon(c, s)
      if (s.typ.sons[0] != nil) and (kind != skIterator): 
        addResult(c, s.typ.sons[0], n.info)
      if sfImportc notin s.flags: 
        # no semantic checking for importc:
        n.sons[codePos] = semStmtScope(c, n.sons[codePos])
      if s.typ.sons[0] != nil and kind != skIterator: addResultNode(c, n)
      popProcCon(c)
    else: 
      if s.typ.sons[0] != nil and kind != skIterator: 
        addDecl(c, newSym(skUnknown, getIdent("result"), nil))
      n.sons[codePos] = semGenericStmtScope(c, n.sons[codePos], {})
    if sfImportc in s.flags: 
      # so we just ignore the body after semantic checking for importc:
      n.sons[codePos] = ast.emptyNode
  else: 
    if proto != nil: LocalError(n.info, errImplOfXexpected, proto.name.s)
    if {sfImportc, sfBorrow} * s.flags == {}: incl(s.flags, sfForward)
    elif sfBorrow in s.flags: semBorrow(c, n, s)
  sideEffectsCheck(c, s)
  closeScope(c.tab)           # close scope for parameters
  popOwner()
  
proc semIterator(c: PContext, n: PNode): PNode = 
  result = semProcAux(c, n, skIterator, iteratorPragmas)
  var s = result.sons[namePos].sym
  var t = s.typ
  if t.sons[0] == nil: 
    LocalError(n.info, errXNeedsReturnType, "iterator")
  if n.sons[codePos].kind == nkEmpty and s.magic == mNone: 
    LocalError(n.info, errImplOfXexpected, s.name.s)
  
proc semProc(c: PContext, n: PNode): PNode = 
  result = semProcAux(c, n, skProc, procPragmas)

proc semMethod(c: PContext, n: PNode): PNode = 
  if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "method")
  result = semProcAux(c, n, skMethod, methodPragmas)
  
  var s = result.sons[namePos].sym
  var t = s.typ
  var hasObjParam = false
  
  for col in countup(1, sonsLen(t)-1): 
    if skipTypes(t.sons[col], skipPtrs).kind == tyObject: 
      hasObjParam = true
      break
  
  # XXX this not really correct way to do it: Perhaps it should be done after
  # generic instantiation. Well it's good enough for now: 
  if not hasObjParam:
    LocalError(n.info, errXNeedsParamObjectType, "method")

proc semConverterDef(c: PContext, n: PNode): PNode = 
  if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "converter")
  checkSonsLen(n, codePos + 1)
  if n.sons[genericParamsPos].kind != nkEmpty: 
    LocalError(n.info, errNoGenericParamsAllowedForX, "converter")
  result = semProcAux(c, n, skConverter, converterPragmas)
  var s = result.sons[namePos].sym
  var t = s.typ
  if t.sons[0] == nil: LocalError(n.info, errXNeedsReturnType, "converter")
  if sonsLen(t) != 2: LocalError(n.info, errXRequiresOneArgument, "converter")
  addConverter(c, s)

proc semMacroDef(c: PContext, n: PNode): PNode = 
  checkSonsLen(n, codePos + 1)
  if n.sons[genericParamsPos].kind != nkEmpty: 
    LocalError(n.info, errNoGenericParamsAllowedForX, "macro")
  result = semProcAux(c, n, skMacro, macroPragmas)
  var s = result.sons[namePos].sym
  var t = s.typ
  if t.sons[0] == nil: LocalError(n.info, errXNeedsReturnType, "macro")
  if sonsLen(t) != 2: LocalError(n.info, errXRequiresOneArgument, "macro")
  if n.sons[codePos].kind == nkEmpty:
    LocalError(n.info, errImplOfXexpected, s.name.s)
  
proc evalInclude(c: PContext, n: PNode): PNode = 
  result = newNodeI(nkStmtList, n.info)
  addSon(result, n)           # the rodwriter needs include information!
  for i in countup(0, sonsLen(n) - 1): 
    var f = getModuleFile(n.sons[i])
    var fileIndex = includeFilename(f)
    if ContainsOrIncl(c.includedFiles, fileIndex): 
      GlobalError(n.info, errRecursiveDependencyX, f)
    addSon(result, semStmt(c, gIncludeFile(f)))
    Excl(c.includedFiles, fileIndex)
  
proc SemStmt(c: PContext, n: PNode): PNode = 
  const                       # must be last statements in a block:
    LastBlockStmts = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt}
  result = n
  if gCmd == cmdIdeTools: 
    suggestStmt(c, n)
  if nfSem in n.flags: return 
  case n.kind
  of nkAsgn: result = semAsgn(c, n)
  of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkMacroStmt, nkCallStrLit: 
    result = semCommand(c, n)
  of nkEmpty, nkCommentStmt, nkNilLit: nil
  of nkBlockStmt: result = semBlock(c, n)
  of nkStmtList: 
    var length = sonsLen(n)
    for i in countup(0, length - 1): 
      n.sons[i] = semStmt(c, n.sons[i])
      if n.sons[i].kind in LastBlockStmts: 
        for j in countup(i + 1, length - 1): 
          case n.sons[j].kind
          of nkPragma, nkCommentStmt, nkNilLit, nkEmpty: nil
          else: localError(n.sons[j].info, errStmtInvalidAfterReturn)
  of nkRaiseStmt: result = semRaise(c, n)
  of nkVarSection: result = semVar(c, n)
  of nkConstSection: result = semConst(c, n)
  of nkTypeSection: result = SemTypeSection(c, n)
  of nkIfStmt: result = SemIf(c, n)
  of nkWhenStmt: result = semWhen(c, n)
  of nkDiscardStmt: result = semDiscard(c, n)
  of nkWhileStmt: result = semWhile(c, n)
  of nkTryStmt: result = semTry(c, n)
  of nkBreakStmt, nkContinueStmt: result = semBreakOrContinue(c, n)
  of nkForStmt: result = semFor(c, n)
  of nkCaseStmt: result = semCase(c, n)
  of nkReturnStmt: result = semReturn(c, n)
  of nkAsmStmt: result = semAsm(c, n)
  of nkYieldStmt: result = semYield(c, n)
  of nkPragma: pragma(c, c.p.owner, n, stmtPragmas)
  of nkIteratorDef: result = semIterator(c, n)
  of nkProcDef: result = semProc(c, n)
  of nkMethodDef: result = semMethod(c, n)
  of nkConverterDef: result = semConverterDef(c, n)
  of nkMacroDef: result = semMacroDef(c, n)
  of nkTemplateDef: result = semTemplateDef(c, n)
  of nkImportStmt: 
    if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "import")
    result = evalImport(c, n)
  of nkFromStmt: 
    if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "from")
    result = evalFrom(c, n)
  of nkIncludeStmt: 
    if not isTopLevel(c): LocalError(n.info, errXOnlyAtModuleScope, "include")
    result = evalInclude(c, n)
  else: 
    # in interactive mode, we embed the expression in an 'echo':
    if gCmd == cmdInteractive:
      result = buildEchoStmt(c, semExpr(c, n))
    else:
      LocalError(n.info, errStmtExpected)
      result = ast.emptyNode
  if result == nil: InternalError(n.info, "SemStmt: result = nil")
  incl(result.flags, nfSem)

proc semStmtScope(c: PContext, n: PNode): PNode = 
  openScope(c.tab)
  result = semStmt(c, n)
  closeScope(c.tab)