about summary refs log tree commit diff stats
path: root/README
blob: 2dcaeddfdc0e18dc8971c71a5c56a9a2e4993e66 (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
dwm - dynamic window manager
============================
dwm is an extremely fast, small, and dynamic window manager for X.


Requirements
------------
In order to build dwm you need the Xlib header files.


Installation
------------
Edit config.mk to match your local setup (dwm is installed into
the /usr/local namespace by default).

Afterwards enter the following command to build and install dwm (if
necessary as root):

    make clean install

If you are going to use the default bluegray color scheme it is highly
recommended to also install the bluegray files shipped in the dextra package.


Running dwm
-----------
Add the following line to your .xinitrc to start dwm using startx:

    exec dwm

In order to connect dwm to a specific display, make sure that
the DISPLAY environment variable is set correctly, e.g.:

    DISPLAY=foo.bar:1 exec dwm

(This will start dwm on display :1 of the host foo.bar.)

In order to display status info in the bar, you can do something
like this in your .xinitrc:

    while true
    do
        echo `date` `uptime | sed 's/.*,//'`
        sleep 1
    done | dwm


Configuration
-------------
The configuration of dwm is done by creating a custom config.h
and (re)compiling the source code.
r IDE support' href='/ahoang/Nim/commit/rod/semtypes.nim?h=devel&id=5b28d08203ba36e84c8e381761db182c0393d07f'>5b28d0820 ^
e25474154 ^

5b28d0820 ^
e25474154 ^
049de0ef6 ^
580d85d23 ^
23ef565a3 ^
e25474154 ^



1a8c6fb49 ^

5b0d8246f ^
1a8c6fb49 ^
b595fc834 ^

438703f59 ^
b595fc834 ^

92b8fac94 ^
b595fc834 ^
92b8fac94 ^
438703f59 ^
1a8c6fb49 ^



5131b3cea ^

e25474154 ^
92b8fac94 ^
b595fc834 ^
1a8c6fb49 ^
e25474154 ^







23ef565a3 ^
580d85d23 ^


92b8fac94 ^
e25474154 ^
3d1c6de63 ^
e25474154 ^
23ef565a3 ^
e25474154 ^

e25474154 ^

6c2050912 ^
049de0ef6 ^
e25474154 ^

b595fc834 ^
92b8fac94 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^
e25474154 ^


e25474154 ^

6c2050912 ^
049de0ef6 ^
e25474154 ^
92b8fac94 ^
b595fc834 ^
064f29621 ^
8805829d7 ^







92b8fac94 ^
8805829d7 ^
4196757de ^






064f29621 ^
4196757de ^






049de0ef6 ^
4196757de ^
e25474154 ^
e25474154 ^
064f29621 ^
6c2050912 ^
b595fc834 ^
92b8fac94 ^
b595fc834 ^
049de0ef6 ^
064f29621 ^

8a0509b3b ^
e25474154 ^
8a0509b3b ^



e25474154 ^
45a345e93 ^
438703f59 ^
8dda362fa ^
e25474154 ^

45a345e93 ^
b731e6ef1 ^
45a345e93 ^
9fb075557 ^


45a345e93 ^
9fb075557 ^



92b8fac94 ^
9fb075557 ^
92b8fac94 ^
9fb075557 ^

45a345e93 ^

9fb075557 ^

45a345e93 ^
9fb075557 ^
45a345e93 ^



9fb075557 ^
e25474154 ^
b595fc834 ^
e25474154 ^
b595fc834 ^
23ef565a3 ^







b595fc834 ^
92b8fac94 ^
79aaf213d ^
b595fc834 ^
92b8fac94 ^
79aaf213d ^
b595fc834 ^
e25474154 ^




8dda362fa ^
ca02dfab4 ^

1ffae7cba ^
49b0440c4 ^
1ffae7cba ^
8682ed9bd ^
027f30610 ^
8682ed9bd ^
b731e6ef1 ^
8682ed9bd ^

37b199d34 ^
4b7655fd1 ^
1ffae7cba ^







37b199d34 ^
abb701082 ^
ca02dfab4 ^
abb701082 ^






abb701082 ^
049de0ef6 ^
e25474154 ^
1ffae7cba ^
fee2a7ecf ^
92b8fac94 ^
b595fc834 ^
92b8fac94 ^
e25474154 ^
049de0ef6 ^
e25474154 ^
92b8fac94 ^
79aaf213d ^
e25474154 ^

e25474154 ^

6c2050912 ^
e25474154 ^

92b8fac94 ^
049de0ef6 ^
b595fc834 ^
92b8fac94 ^
79aaf213d ^
dfb5cb3c5 ^
4741e8f9a ^
fc6cc7927 ^

4741e8f9a ^
92b8fac94 ^
fc6cc7927 ^

22dc76a36 ^
238202ccb ^
8b933e470 ^

b731e6ef1 ^
251c44ff0 ^
8b933e470 ^
5b5b02cb1 ^

92b8fac94 ^
5b5b02cb1 ^
dfb5cb3c5 ^


37b199d34 ^











c9513c2e5 ^


92b8fac94 ^
c9513c2e5 ^


b595fc834 ^
92b8fac94 ^
c7158af75 ^
fc6cc7927 ^

b4844a189 ^
46c41e436 ^
fc6cc7927 ^

b4844a189 ^
fc6cc7927 ^
92b8fac94 ^
c7158af75 ^
e25474154 ^

064f29621 ^

e25474154 ^

ade67f1ab ^
5b28d0820 ^
e25474154 ^
5b28d0820 ^
92b8fac94 ^
e25474154 ^
5b28d0820 ^


b595fc834 ^
92b8fac94 ^
b595fc834 ^
5b28d0820 ^
92b8fac94 ^
e25474154 ^
5e15dec17 ^
e25474154 ^


92b8fac94 ^

b595fc834 ^


b731e6ef1 ^
e25474154 ^
13c69f554 ^

e25474154 ^
e25474154 ^
6c2050912 ^
5e15dec17 ^


40a5d6c3b ^
5131b3cea ^


92b8fac94 ^
5131b3cea ^
e25474154 ^
5131b3cea ^
5e15dec17 ^
e25474154 ^








13c69f554 ^

7fcbdc6d4 ^
13c69f554 ^
438703f59 ^
7fcbdc6d4 ^
e25474154 ^
b731e6ef1 ^
e25474154 ^
d5a5c2291 ^


e25474154 ^
d5a5c2291 ^

92b8fac94 ^
e25474154 ^
92b8fac94 ^
e25474154 ^
a79acb58f ^

02b1503e1 ^

a79acb58f ^



92b8fac94 ^
b595fc834 ^
e25474154 ^
2df9b442c ^
92b8fac94 ^
8b6f9ef5e ^
a79acb58f ^
8b6f9ef5e ^

92b8fac94 ^
8b6f9ef5e ^

a79acb58f ^
8b6f9ef5e ^

a79acb58f ^
8b6f9ef5e ^




92b8fac94 ^
e25474154 ^
7dff3e64b ^
65970efd9 ^


8b6f9ef5e ^
8dda362fa ^
8b6f9ef5e ^



c6d061111 ^
8b6f9ef5e ^










d5a5c2291 ^

23ef565a3 ^



7ad5cab17 ^
e25474154 ^

23ef565a3 ^
674c05f42 ^
b0c11d3ef ^
e25474154 ^
92b8fac94 ^
b731e6ef1 ^
23ef565a3 ^
92b8fac94 ^
23ef565a3 ^
92b8fac94 ^
23ef565a3 ^
92b8fac94 ^
7ad5cab17 ^
23ef565a3 ^
7ad5cab17 ^
d5a5c2291 ^
e25474154 ^
23ef565a3 ^
e25474154 ^

23ef565a3 ^
e25474154 ^




e25474154 ^
5b28d0820 ^
e25474154 ^


23ef565a3 ^
72651de71 ^
e25474154 ^
72651de71 ^



e25474154 ^
72651de71 ^
e25474154 ^
72651de71 ^
e25474154 ^
b731e6ef1 ^
72651de71 ^
92b8fac94 ^
674c05f42 ^
72651de71 ^


e25474154 ^

72651de71 ^
e25474154 ^
b731e6ef1 ^
72651de71 ^




92b8fac94 ^
72651de71 ^
b731e6ef1 ^
72651de71 ^



e25474154 ^


23ef565a3 ^
e25474154 ^
5b28d0820 ^
e25474154 ^


72651de71 ^
e25474154 ^
72651de71 ^

23ef565a3 ^
72651de71 ^


b595fc834 ^
72651de71 ^
92b8fac94 ^
b595fc834 ^


23ef565a3 ^

b595fc834 ^
72651de71 ^
5e15dec17 ^
e25474154 ^

23ef565a3 ^
e25474154 ^

23ef565a3 ^
e25474154 ^
92b8fac94 ^
5b28d0820 ^

e25474154 ^
5b28d0820 ^
73c6efdf6 ^
e25474154 ^


23ef565a3 ^
e25474154 ^
23ef565a3 ^
92b8fac94 ^
e25474154 ^
23ef565a3 ^
e25474154 ^
23ef565a3 ^
e25474154 ^

23ef565a3 ^

e25474154 ^
23ef565a3 ^
92b8fac94 ^
e25474154 ^
92b8fac94 ^
e25474154 ^






dc669155e ^






064f29621 ^

ade67f1ab ^
dc669155e ^


e25474154 ^
5b28d0820 ^
2f066395b ^
b40ab4dc9 ^
dc669155e ^


79aaf213d ^


92b8fac94 ^
e25474154 ^
049de0ef6 ^
e25474154 ^
23ef565a3 ^
cb79bf9f1 ^




d17caa86e ^

027f30610 ^
4b7655fd1 ^









be1154106 ^
5aa486cf1 ^



4b7655fd1 ^

5aa486cf1 ^
4b7655fd1 ^
5aa486cf1 ^
4b7655fd1 ^

5aa486cf1 ^





46e9ff613 ^
5aa486cf1 ^
be1154106 ^
56d75bd23 ^

72291875b ^
73c6efdf6 ^
72291875b ^

dfb5cb3c5 ^


8e0941576 ^

dfb5cb3c5 ^


1d02f2ea5 ^



dfb5cb3c5 ^




f05ac0cb2 ^


dfb5cb3c5 ^

1ffae7cba ^
dfb5cb3c5 ^


ac271e76b ^
8e0941576 ^
dfb5cb3c5 ^







e6b0b7ecc ^



dfb5cb3c5 ^


296ef0795 ^
027f30610 ^

66a255652 ^
027f30610 ^


f0953db3b ^

e6b0b7ecc ^



f0953db3b ^
66a255652 ^
22dc76a36 ^
dfb5cb3c5 ^
56d75bd23 ^
cda92048b ^
56d75bd23 ^
e6b0b7ecc ^

66a255652 ^
bc2eb0ea9 ^
dfb5cb3c5 ^


66a255652 ^
8e0941576 ^

dfb5cb3c5 ^
d912d1837 ^
dfb5cb3c5 ^



d912d1837 ^
66a255652 ^

dfb5cb3c5 ^






a59f13b00 ^
bc2eb0ea9 ^
a27eb5153 ^


754e2ef1d ^

ee1b0d8c6 ^




37b199d34 ^
a27eb5153 ^



085b339b8 ^









dfb5cb3c5 ^
a6a18be08 ^




dfb5cb3c5 ^




72291875b ^
de3b7cd41 ^
49b0440c4 ^
66a255652 ^

72291875b ^
754e2ef1d ^







49b0440c4 ^
754e2ef1d ^
3f71b7f1f ^
a59f13b00 ^

952dbc4b8 ^
66a255652 ^
754e2ef1d ^
a59f13b00 ^
1d02f2ea5 ^
ac271e76b ^
37b199d34 ^


ac271e76b ^
73c6efdf6 ^
251c44ff0 ^
dfb5cb3c5 ^
251c44ff0 ^
7171ae62c ^






37b199d34 ^

be1154106 ^
70f5236d4 ^
ade67f1ab ^
e25474154 ^



dd99fe61c ^
ade67f1ab ^
049de0ef6 ^
2133fbfcc ^



e439c6b02 ^
ade67f1ab ^
6c2050912 ^
3a5cf3d63 ^
6c2050912 ^
92b8fac94 ^
e25474154 ^
70f5236d4 ^

7171ae62c ^

70f5236d4 ^


70f5236d4 ^
7171ae62c ^
dfb5cb3c5 ^
70f5236d4 ^
dd99fe61c ^
6c2050912 ^
dd99fe61c ^
e25474154 ^
3005955d2 ^

e424e13bd ^

fd62116f6 ^

70f5236d4 ^
027f30610 ^


4f7fa0591 ^
dd99fe61c ^
5e15dec17 ^
dfb5cb3c5 ^


251c44ff0 ^
e25474154 ^
7171ae62c ^
e25474154 ^
70f5236d4 ^
92b8fac94 ^

e25474154 ^
049de0ef6 ^
be1154106 ^
b731e6ef1 ^

518b79449 ^
bc2eb0ea9 ^
518b79449 ^




00da785f5 ^


251c44ff0 ^
dfb5cb3c5 ^


7e4401549 ^
5324c9ebb ^
085b339b8 ^




5324c9ebb ^

56d75bd23 ^
37b199d34 ^

f0953db3b ^
37b199d34 ^


12bac28d2 ^
e25474154 ^
e439c6b02 ^
12bac28d2 ^
e25474154 ^
12bac28d2 ^
e25474154 ^


12bac28d2 ^
e25474154 ^


92b8fac94 ^
e25474154 ^
9a6f47ae6 ^
5e15dec17 ^
e25474154 ^



9a6f47ae6 ^
92b8fac94 ^
e25474154 ^
fd62116f6 ^
fd62116f6 ^

4b7655fd1 ^
b3da0ab88 ^




4b7655fd1 ^



fd62116f6 ^
4b7655fd1 ^
3f71b7f1f ^
f760bc243 ^

92b8fac94 ^
f760bc243 ^


4b7655fd1 ^
f760bc243 ^


4b7655fd1 ^

51076a0be ^



f760bc243 ^
4b7655fd1 ^
f760bc243 ^


4b7655fd1 ^
3e79e9f98 ^
4b7655fd1 ^
92b8fac94 ^
f760bc243 ^


754e2ef1d ^
f760bc243 ^



f0953db3b ^
f760bc243 ^
4b7655fd1 ^

92b8fac94 ^
f760bc243 ^

8e0941576 ^

fd62116f6 ^
badb6c0f6 ^
7b36d3d6f ^
66a255652 ^

12bac28d2 ^
92b8fac94 ^
12bac28d2 ^
8948a9715 ^

f96ec25ed ^
8948a9715 ^


6378fbd66 ^

a6a18be08 ^
31dd66acb ^
6378fbd66 ^









862c0ef83 ^
















22dc76a36 ^
e25474154 ^
f8dd74a07 ^
e25474154 ^
73c6efdf6 ^
da6046dcb ^

8d734244b ^
085b339b8 ^

e25474154 ^

b595fc834 ^
ccd2934e4 ^
92b8fac94 ^
79aaf213d ^
12bac28d2 ^
b1d4dfa6b ^


9c8bc3a24 ^

ccd2934e4 ^
9c8bc3a24 ^


1d02f2ea5 ^
92b8fac94 ^
9c8bc3a24 ^
1d02f2ea5 ^
92b8fac94 ^
9c8bc3a24 ^

1d02f2ea5 ^

ccd2934e4 ^
1d02f2ea5 ^






73c6efdf6 ^
1d02f2ea5 ^


ccd2934e4 ^
92b8fac94 ^
bc2eb0ea9 ^
9c8bc3a24 ^
bc2eb0ea9 ^
badb6c0f6 ^
12bac28d2 ^




e25474154 ^
dc669155e ^
e25474154 ^






8805829d7 ^
cda92048b ^

af7c92c00 ^




e25474154 ^
ac271e76b ^










dc669155e ^
b595fc834 ^
92b8fac94 ^
79aaf213d ^
5b5b02cb1 ^
c1f1f8419 ^

b595fc834 ^
e25474154 ^


3f82352c2 ^



e25474154 ^
b595fc834 ^
dd99fe61c ^
dc669155e ^
e25474154 ^





b595fc834 ^
92b8fac94 ^
79aaf213d ^
6c2050912 ^

6378fbd66 ^
064f29621 ^

6c2050912 ^

027f30610 ^
edab4aaad ^



862c0ef83 ^
ccd2934e4 ^
862c0ef83 ^
8d99753d6 ^
862c0ef83 ^
085b339b8 ^





862c0ef83 ^




dc669155e ^



ccd2934e4 ^


8948a9715 ^
ccd2934e4 ^
7d6500f1d ^
92b8fac94 ^
79aaf213d ^
e25474154 ^



4741e8f9a ^
e25474154 ^

4741e8f9a ^
6c2050912 ^




e2d38a57e ^




6c2050912 ^


e2d38a57e ^
6c2050912 ^

e25474154 ^

049de0ef6 ^
e25474154 ^

049de0ef6 ^
6c2050912 ^
e25474154 ^

049de0ef6 ^
4741e8f9a ^
6c2050912 ^


d912d1837 ^


6a8a409f1 ^
d912d1837 ^















73c6efdf6 ^
92b8fac94 ^
13c69f554 ^
f760bc243 ^
1d02f2ea5 ^
4741e8f9a ^


674c05f42 ^
5e15dec17 ^
674c05f42 ^
f760bc243 ^
4741e8f9a ^

f760bc243 ^


4741e8f9a ^
f760bc243 ^


027f30610 ^
56d75bd23 ^
d912d1837 ^

f760bc243 ^




4741e8f9a ^
f760bc243 ^
027f30610 ^
4741e8f9a ^
f760bc243 ^




37b199d34 ^
1d02f2ea5 ^


f760bc243 ^






37b199d34 ^
f760bc243 ^
4741e8f9a ^
f760bc243 ^
4741e8f9a ^
049de0ef6 ^
f760bc243 ^
4741e8f9a ^
5e15dec17 ^
f760bc243 ^
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333


                               
                                         



                                                   
 
                                                             
                       
 





                                                                       

                                                       
                                          
                                         


                                                         
     


                          




                                         
                                

                                                 
                                                             
                               
                         
                                                               
                     



                                                    

                                                
                                                            
                  

                                                                    
                                                                               

                                                                        
                                                             
             
                                                       
                             



                          

                                                           
                        
                                                                      
                     
                                   







                                            
                                      


                                                            
                                                 
                                   
                                                           
                
                                                 

                                                         

                                        
                                             
                                  

                                                       
                                  
                                                  
                                             
                                        
       
                                                      
                                          


                                                                           

                                       
                                             
                                  
        
                                                        
                                          
 







                                                                     
                                                          
                                          






                                                                            
                                         






                                                                            
                                  
 
                                                             
                      
                                          
                                             
                           
                                                 
                         
                                  

                                    
 
                                                              



                                                          
  
                                                             
                   
                    

                                          
                                                      
                                       
  


                                                        
  



                                                                          
                                           
                                   
                                              

                                                              

                

                                                 
         
                                               



                                                    
                                     
 
                                                          
              
                     







                                                                
         
                                                  
                                              
       
                                                        
                                            
 




                                                           
                                                      

                                                              
                                  
                                                  
                                             
                                                             
                                                      
                                                               
                                             

                                                       
                                            
                                    







                                                                 
                                            
                             
                                            






                                                             
                                                      
                                  
                                                       
                                                              
                                 
                                                          
                               
                                                                     
                                         
                                  
        
                                                    
                                            

                                                             

                                            
                                             

                                    
                                                          
                                  
       
                                                          
                                            
 
                                                

                      
       
                                                                     

                         
                                                                  
                                                        

                                                             
                               
                                            
                                       

                                 
                                             
                               


                                                                 











                                                                        


                                                                   
                                            


                                                
             
                                                                        
                               

                                           
                            
                  

                      
                        
         
                                               
                             

                                                           

                                                     

                                          
                          
                 
                                       
                     
                                               
                         


                                                   
         
                                         
                        
                                           
                                                                
                                     
                                                


                              

                                                                      


                                           
                                                      
 

                                                        
                              
                          
                                                      


                                                                      
                             


                                                       
                                                              
         
                     
       
                                








                                                                      

                                                          
                                                        
                                                          
                 
       
                                             
                                           
  


                                                                          
                                                 

                                                   
                                                  
  
                                                                                  
                       

                             

                                                         



                                    
                                                            
                                                                 
 
                                                  
                                                          
                    
                                                              

                                                    
                                                            

                      
                                                                

                         
                                                                




                                                                    
                                              
                                            
                          


                         
                                                           
         



                                                                       
                                                               










                                                                       

                                         



                                                                              
                     

                                                        
                             
                                                             
          
                                           
                             
                                                              
                            
                                                
                         
                                                                      
                                   
                                                            
                        
                                      
                               
                
                       
                  

                                        
              




                                                                   
                                                            
                                             


                                                                               
                                                      
                     
             



                                                  
                                   
                 
                  
                      
                           
                                   
                                                 
                                                                          
                                                                   


                                                                   

                                             
               
                           
                                




                                                      
                                                                          
                                                           
                              



                                                              


                                                                            
               
                                                  
                                                                 


                                                            
                 
                         

                           
                                                                              


                                                              
                  
                                         
                                         


                                                 

                                    
                                      
                                                                     
                              

                      
                                                                        

                                  
                                                                
              
                                          

                                                                  
                                    
                                           
                     


                                                                          
                                      
             
               
                                                                                
                                                   
                                        
                         
                            

                                                                              

                                        
                                                     
           
                              
            
                                                        






                                                                       






                                                         

                                                               
                          


                                                                      
                    
                                
                                                                      
                                                                      


                                                                           


                                                                         
                                                                 
                                           
                         
                                        
                                                              




                                                                       

                                                      
 









                                                     
                                                                 



                                            

                                                      
                            
                             
                

                                      





                                                             
       
                  
 

                                   
                            
                                     

                                       


                                                                         

                                                       


                                                                       



                                                                      




                                                                      


                                                      

                                         
                                            


                                       
                 
 







                                                                             



                                        


                                              
                      

                                                            
  


                                                   

                                                               



                                                                     
                                                  
  
                
                                          
                                              
                                                                
                                                         

                                                         
  
                


                                                        
  

                                             
                                                  
                                                  



                                                      
                                                                         

                                                                 






                                                   
  
                   


                                             

                                                                           




                                                 
   



                                                                           









                                                          
                   




                                                         




                                                 
                                     
 
                                                       

                       
                           







                                                              
                                        
 
                                                             

                                                                          
            
                                            
                                                          
  
                    
                                          


                                     
    
               
 
                                   
 






                                                                        

                                                          
     
              
               



                                                
                                                          
                     
                                      



                                                                           
                       
                          
                 
                                 
                     
                                             
                         

                      

                             


                                                   
               
                                                         
 
                  
                                                 
                                                       
                    
                     

                                                        

                                                     

                                        
                                   


                                                                          
                                                                
                                   
                                              


                                                                   
                         
                            
                                 
                  
                                                                    

                                                                    
                                       
                                  
                                    

                                                    
              
                               




                                      


                                                                          
                                                  


                                                                       
                              
                       




                                                                       

                        
 

                           
                                       


                                       
                                                                 
                       
                         
                                  
                                     
                


                                                     
       


                                                               
                             
                    
              
                                           



                                              
               
                             
 
                                                                

                                 
                                                                     




                                                               



                                                                     
                                                      
                             
 

                             
                          


                                       
                         


                                                          

                               



                                                               
       
                              


                                 
                                                                
                                                       
                                                                 
                                           


                                            
 



                                                     
   
                  

                                                                
                                                           

                                                

                                                             
 
                                                
                                                  

                              
       
                                                     
 

                                                    
                                                   


                

                                                              
                                                  
              









                                      
















                                                                   
                                                             
              
                                           
             
                     

                                                       
                      

                                                             

                                                                
         
                                   
                                         
                                              
                 


                                      

                                                       
                          


                                             
                     
                                                     
                                                  
                       
                                                     

                                                  

                                                                
                              






                                                                        
                                                            


                                                       
             
                                                          
           
                                  
         
                                




                                                                      
                         
                                      






                                                                              
                                                

                                                                     




                                                          
                                            










                                                         
                              
                     
                                                               
                                              
                                                        

                                                              
                     


                             



                                                                               
                   
           
                                                 
                       





                           
         
                                                                   
                                              

                                                   
                                                     

                                                   

                                                   
                



                                             
                  
                      
                                       
         
                                                                  





                                                                      




                                                       



                                                         


                                            
                                    
                               
       
                                       
                                            



                                                         
                   

                                              
              




                                          




                                            


                                                
                                               

                                      

                                      
                                        

                                       
                                        
                                                  

                             
                                          
                                                       


                                          


                                         
                                           















                                         
                          
                                           
  
                                                          
                                                   


                                                                              
                                
                   
          
                                    

                                             


                          
                  


                                           
                                              
                                  

                                                                        




                                             
                    
                                      
                                                        
           




                                            
                                                 


                                     






                                                              
                                                                       
                                                                 
             
                                                         
                                         
                                                       
                             
                                   
                                              
 
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# this module does the semantic checking of type declarations
# included from sem.nim

proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType = 
  if prev == nil: 
    result = newTypeS(kind, c)
  else: 
    result = prev
    if result.kind == tyForward: result.kind = kind

proc newConstraint(c: PContext, k: TTypeKind): PType = 
  result = newTypeS(tyBuiltInTypeClass, c)
  result.addSonSkipIntLit(newTypeS(k, c))

proc semEnum(c: PContext, n: PNode, prev: PType): PType =
  if n.sonsLen == 0: return newConstraint(c, tyEnum)
  var
    counter, x: BiggestInt
    e: PSym
    base: PType
  counter = 0
  base = nil
  result = newOrPrevType(tyEnum, prev, c)
  result.n = newNodeI(nkEnumTy, n.info)
  checkMinSonsLen(n, 1)
  if n.sons[0].kind != nkEmpty: 
    base = semTypeNode(c, n.sons[0].sons[0], nil)
    if base.kind != tyEnum: 
      localError(n.sons[0].info, errInheritanceOnlyWithEnums)
    counter = lastOrd(base) + 1
  rawAddSon(result, base)
  let isPure = result.sym != nil and sfPure in result.sym.flags
  var hasNull = false
  for i in countup(1, sonsLen(n) - 1): 
    case n.sons[i].kind
    of nkEnumFieldDef: 
      e = newSymS(skEnumField, n.sons[i].sons[0], c)
      var v = semConstExpr(c, n.sons[i].sons[1])
      var strVal: PNode = nil
      case skipTypes(v.typ, abstractInst-{tyTypeDesc}).kind 
      of tyTuple: 
        if sonsLen(v) == 2:
          strVal = v.sons[1] # second tuple part is the string value
          if skipTypes(strVal.typ, abstractInst).kind in {tyString, tyCString}:
            x = getOrdValue(v.sons[0]) # first tuple part is the ordinal
          else:
            localError(strVal.info, errStringLiteralExpected)
        else:
          localError(v.info, errWrongNumberOfVariables)
      of tyString, tyCString:
        strVal = v
        x = counter
      else:
        x = getOrdValue(v)
      if i != 1:
        if x != counter: incl(result.flags, tfEnumHasHoles)
        if x < counter: 
          localError(n.sons[i].info, errInvalidOrderInEnumX, e.name.s)
          x = counter
      e.ast = strVal # might be nil
      counter = x
    of nkSym: 
      e = n.sons[i].sym
    of nkIdent: 
      e = newSymS(skEnumField, n.sons[i], c)
    else: illFormedAst(n)
    e.typ = result
    e.position = int(counter)
    if e.position == 0: hasNull = true
    if result.sym != nil and sfExported in result.sym.flags:
      incl(e.flags, sfUsed)
      incl(e.flags, sfExported)
      if not isPure: strTableAdd(c.module.tab, e)
    addSon(result.n, newSymNode(e))
    if sfGenSym notin e.flags and not isPure: addDecl(c, e)
    inc(counter)
  if not hasNull: incl(result.flags, tfNeedsInit)

proc semSet(c: PContext, n: PNode, prev: PType): PType = 
  result = newOrPrevType(tySet, prev, c)
  if sonsLen(n) == 2: 
    var base = semTypeNode(c, n.sons[1], nil)
    addSonSkipIntLit(result, base)
    if base.kind == tyGenericInst: base = lastSon(base)
    if base.kind != tyGenericParam: 
      if not isOrdinalType(base): 
        localError(n.info, errOrdinalTypeExpected)
      elif lengthOrd(base) > MaxSetElements: 
        localError(n.info, errSetTooBig)
  else:
    localError(n.info, errXExpectsOneTypeParam, "set")
    addSonSkipIntLit(result, errorType(c))
  
proc semContainer(c: PContext, n: PNode, kind: TTypeKind, kindStr: string, 
                  prev: PType): PType = 
  result = newOrPrevType(kind, prev, c)
  if sonsLen(n) == 2: 
    var base = semTypeNode(c, n.sons[1], nil)
    addSonSkipIntLit(result, base)
  else: 
    localError(n.info, errXExpectsOneTypeParam, kindStr)
    addSonSkipIntLit(result, errorType(c))

proc semVarargs(c: PContext, n: PNode, prev: PType): PType =
  result = newOrPrevType(tyVarargs, prev, c)
  if sonsLen(n) == 2 or sonsLen(n) == 3:
    var base = semTypeNode(c, n.sons[1], nil)
    addSonSkipIntLit(result, base)
    if sonsLen(n) == 3:
      result.n = newIdentNode(considerAcc(n.sons[2]), n.sons[2].info)
  else:
    localError(n.info, errXExpectsOneTypeParam, "varargs")
    addSonSkipIntLit(result, errorType(c))

proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType =
  if n.len < 1:
    result = newConstraint(c, kind)
  else:
    let n = if n[0].kind == nkBracket: n[0] else: n
    checkMinSonsLen(n, 1)
    result = newOrPrevType(kind, prev, c)
    # check every except the last is an object:
    for i in 0 .. n.len-2:
      let region = semTypeNode(c, n[i], nil)
      if region.skipTypes({tyGenericInst}).kind notin {tyError, tyObject}:
        message n[i].info, errGenerated, "region needs to be an object type"
      addSonSkipIntLit(result, region)
    var base = semTypeNode(c, n.lastSon, nil)
    addSonSkipIntLit(result, base)

proc semVarType(c: PContext, n: PNode, prev: PType): PType = 
  if sonsLen(n) == 1: 
    result = newOrPrevType(tyVar, prev, c)
    var base = semTypeNode(c, n.sons[0], nil)
    if base.kind == tyVar: 
      localError(n.info, errVarVarTypeNotAllowed)
      base = base.sons[0]
    addSonSkipIntLit(result, base)
  else:
    result = newConstraint(c, tyVar)

proc semDistinct(c: PContext, n: PNode, prev: PType): PType = 
  if n.len == 0: return newConstraint(c, tyDistinct)
  result = newOrPrevType(tyDistinct, prev, c)
  addSonSkipIntLit(result, semTypeNode(c, n.sons[0], nil))
  if n.len > 1: result.n = n[1]
  
proc semRangeAux(c: PContext, n: PNode, prev: PType): PType =
  assert isRange(n)
  checkSonsLen(n, 3)
  result = newOrPrevType(tyRange, prev, c)
  result.n = newNodeI(nkRange, n.info)
  if (n[1].kind == nkEmpty) or (n[2].kind == nkEmpty):
    localError(n.info, errRangeIsEmpty)
  
  var range: array[2, PNode]
  range[0] = semExprWithType(c, n[1], {efDetermineType})
  range[1] = semExprWithType(c, n[2], {efDetermineType})
  
  var rangeT: array[2, PType]
  for i in 0..1: rangeT[i] = range[i].typ.skipTypes({tyStatic}).skipIntLit

  if not sameType(rangeT[0], rangeT[1]):
    localError(n.info, errPureTypeMismatch)
  elif not rangeT[0].isOrdinalType:
    localError(n.info, errOrdinalTypeExpected)
  elif enumHasHoles(rangeT[0]):
    localError(n.info, errEnumXHasHoles, rangeT[0].sym.name.s)
  
  for i in 0..1:
    if hasGenericArguments(range[i]):
      result.n.addSon makeStaticExpr(c, range[i])
    else:
      result.n.addSon semConstExpr(c, range[i])
 
  if weakLeValue(result.n[0], result.n[1]) == impNo:
    localError(n.info, errRangeIsEmpty)

  addSonSkipIntLit(result, rangeT[0])

proc semRange(c: PContext, n: PNode, prev: PType): PType =
  result = nil
  if sonsLen(n) == 2:
    if isRange(n[1]):
      result = semRangeAux(c, n[1], prev)
      let n = result.n
      if n.sons[0].kind in {nkCharLit..nkUInt64Lit}:
        if n.sons[0].intVal > 0 or n.sons[1].intVal < 0:
          incl(result.flags, tfNeedsInit)
      elif n.sons[0].floatVal > 0.0 or n.sons[1].floatVal < 0.0:
        incl(result.flags, tfNeedsInit)
    else:
      localError(n.sons[0].info, errRangeExpected)
      result = newOrPrevType(tyError, prev, c)
  else:
    localError(n.info, errXExpectsOneTypeParam, "range")
    result = newOrPrevType(tyError, prev, c)

proc semArray(c: PContext, n: PNode, prev: PType): PType = 
  var indx, base: PType
  result = newOrPrevType(tyArray, prev, c)
  if sonsLen(n) == 3: 
    # 3 = length(array indx base)
    if isRange(n[1]): indx = semRangeAux(c, n[1], nil)
    else:
      let e = semExprWithType(c, n.sons[1], {efDetermineType})
      if e.typ.kind == tyFromExpr:
        indx = makeRangeWithStaticExpr(c, e.typ.n)
      elif e.kind in {nkIntLit..nkUInt64Lit}:
        indx = makeRangeType(c, 0, e.intVal-1, n.info, e.typ)
      elif e.kind == nkSym and e.typ.kind == tyStatic:
        if e.sym.ast != nil: return semArray(c, e.sym.ast, nil)
        internalAssert c.inGenericContext > 0
        if not isOrdinalType(e.typ.lastSon):
          localError(n[1].info, errOrdinalTypeExpected)
        indx = makeRangeWithStaticExpr(c, e)
        indx.flags.incl tfUnresolved
      elif e.kind in nkCallKinds and hasGenericArguments(e):
        if not isOrdinalType(e.typ):
          localError(n[1].info, errOrdinalTypeExpected)
        # This is an int returning call, depending on an
        # yet unknown generic param (see tgenericshardcases).
        # We are going to construct a range type that will be
        # properly filled-out in semtypinst (see how tyStaticExpr
        # is handled there).
        indx = makeRangeWithStaticExpr(c, e)
      elif e.kind == nkIdent:
        indx = e.typ.skipTypes({tyTypeDesc})
      else:
        let x = semConstExpr(c, e)
        if x.kind in {nkIntLit..nkUInt64Lit}:
          indx = makeRangeType(c, 0, x.intVal-1, n.info, 
                               x.typ.skipTypes({tyTypeDesc}))
        else:
          indx = x.typ.skipTypes({tyTypeDesc})
          #localError(n[1].info, errConstExprExpected)
    addSonSkipIntLit(result, indx)
    if indx.kind == tyGenericInst: indx = lastSon(indx)
    if indx.kind notin {tyGenericParam, tyStatic, tyFromExpr}:
      if not isOrdinalType(indx):
        localError(n.sons[1].info, errOrdinalTypeExpected)
      elif enumHasHoles(indx): 
        localError(n.sons[1].info, errEnumXHasHoles, indx.sym.name.s)
    base = semTypeNode(c, n.sons[2], nil)
    addSonSkipIntLit(result, base)
  else: 
    localError(n.info, errArrayExpectsTwoTypeParams)
    result = newOrPrevType(tyError, prev, c)
  
proc semOrdinal(c: PContext, n: PNode, prev: PType): PType = 
  result = newOrPrevType(tyOrdinal, prev, c)
  if sonsLen(n) == 2: 
    var base = semTypeNode(c, n.sons[1], nil)
    if base.kind != tyGenericParam: 
      if not isOrdinalType(base): 
        localError(n.sons[1].info, errOrdinalTypeExpected)
    addSonSkipIntLit(result, base)
  else:
    localError(n.info, errXExpectsOneTypeParam, "ordinal")
    result = newOrPrevType(tyError, prev, c)

proc semTypeIdent(c: PContext, n: PNode): PSym =
  if n.kind == nkSym: 
    result = n.sym
  else:
    result = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared})
    if result != nil:
      markUsed(n, result)
      if result.kind == skParam and result.typ.kind == tyTypeDesc:
        # This is a typedesc param. is it already bound?
        # it's not bound when it's used multiple times in the
        # proc signature for example
        if c.inGenericInst > 0:
          let bound = result.typ.sons[0].sym
          if bound != nil: return bound
          return result
        if result.typ.sym == nil:
          localError(n.info, errTypeExpected)
          return errorSym(c, n)
        result = result.typ.sym.copySym
        result.typ = copyType(result.typ, result.typ.owner, true)
        result.typ.flags.incl tfUnresolved
      
      if result.kind == skGenericParam:
        if result.typ.kind == tyGenericParam and result.typ.len == 0 and
           tfWildcard in result.typ.flags:
          # collapse the wild-card param to a type
          result.kind = skType
          result.typ.flags.excl tfWildcard
          return
        else:
          localError(n.info, errTypeExpected)
          return errorSym(c, n)

      if result.kind != skType: 
        # this implements the wanted ``var v: V, x: V`` feature ...
        var ov: TOverloadIter
        var amb = initOverloadIter(ov, c, n)
        while amb != nil and amb.kind != skType:
          amb = nextOverloadIter(ov, c, n)
        if amb != nil: result = amb
        else:
          if result.kind != skError: localError(n.info, errTypeExpected)
          return errorSym(c, n)
      if result.typ.kind != tyGenericParam:
        # XXX get rid of this hack!
        var oldInfo = n.info
        reset(n[])
        n.kind = nkSym
        n.sym = result
        n.info = oldInfo
    else:
      localError(n.info, errIdentifierExpected)
      result = errorSym(c, n)
  
proc semTuple(c: PContext, n: PNode, prev: PType): PType = 
  if n.sonsLen == 0: return newConstraint(c, tyTuple)
  var typ: PType
  result = newOrPrevType(tyTuple, prev, c)
  result.n = newNodeI(nkRecList, n.info)
  var check = initIntSet()
  var counter = 0
  for i in countup(0, sonsLen(n) - 1): 
    var a = n.sons[i]
    if (a.kind != nkIdentDefs): illFormedAst(a)
    checkMinSonsLen(a, 3)
    var length = sonsLen(a)
    if a.sons[length - 2].kind != nkEmpty: 
      typ = semTypeNode(c, a.sons[length - 2], nil)
    else:
      localError(a.info, errTypeExpected)
      typ = errorType(c)
    if a.sons[length - 1].kind != nkEmpty: 
      localError(a.sons[length - 1].info, errInitHereNotAllowed)
    for j in countup(0, length - 3): 
      var field = newSymG(skField, a.sons[j], c)
      field.typ = typ
      field.position = counter
      inc(counter)
      if containsOrIncl(check, field.name.id): 
        localError(a.sons[j].info, errAttemptToRedefine, field.name.s)
      else:
        addSon(result.n, newSymNode(field))
        addSonSkipIntLit(result, typ)
      if gCmd == cmdPretty: checkDef(a.sons[j], field)

proc semIdentVis(c: PContext, kind: TSymKind, n: PNode, 
                 allowed: TSymFlags): PSym = 
  # identifier with visibility
  if n.kind == nkPostfix: 
    if sonsLen(n) == 2 and n.sons[0].kind == nkIdent: 
      # for gensym'ed identifiers the identifier may already have been
      # transformed to a symbol and we need to use that here:
      result = newSymG(kind, n.sons[1], c)
      var v = n.sons[0].ident
      if sfExported in allowed and v.id == ord(wStar): 
        incl(result.flags, sfExported)
      else:
        localError(n.sons[0].info, errInvalidVisibilityX, v.s)
    else:
      illFormedAst(n)
  else:
    result = newSymG(kind, n, c)
  
proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode, 
                        allowed: TSymFlags): PSym = 
  if n.kind == nkPragmaExpr: 
    checkSonsLen(n, 2)
    result = semIdentVis(c, kind, n.sons[0], allowed)
    case kind
    of skType: 
      # process pragmas later, because result.typ has not been set yet
    of skField: pragma(c, result, n.sons[1], fieldPragmas)
    of skVar:   pragma(c, result, n.sons[1], varPragmas)
    of skLet:   pragma(c, result, n.sons[1], letPragmas)
    of skConst: pragma(c, result, n.sons[1], constPragmas)
    else: discard
  else:
    result = semIdentVis(c, kind, n, allowed)
  if gCmd == cmdPretty: checkDef(n, result)
  
proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) =
  let ex = t[branchIndex][currentEx].skipConv
  for i in countup(1, branchIndex):
    for j in countup(0, sonsLen(t.sons[i]) - 2): 
      if i == branchIndex and j == currentEx: break
      if overlap(t.sons[i].sons[j].skipConv, ex):
        localError(ex.info, errDuplicateCaseLabel)
  
proc semBranchRange(c: PContext, t, a, b: PNode, covered: var BiggestInt): PNode =
  checkMinSonsLen(t, 1)
  let ac = semConstExpr(c, a)
  let bc = semConstExpr(c, b)
  let at = fitNode(c, t.sons[0].typ, ac).skipConvTakeType
  let bt = fitNode(c, t.sons[0].typ, bc).skipConvTakeType
  
  result = newNodeI(nkRange, a.info)
  result.add(at)
  result.add(bt)
  if emptyRange(ac, bc): localError(b.info, errRangeIsEmpty)
  else: covered = covered + getOrdValue(bc) - getOrdValue(ac) + 1

proc semCaseBranchRange(c: PContext, t, b: PNode, 
                        covered: var BiggestInt): PNode = 
  checkSonsLen(b, 3)
  result = semBranchRange(c, t, b.sons[1], b.sons[2], covered)

proc semCaseBranchSetElem(c: PContext, t, b: PNode, 
                          covered: var BiggestInt): PNode = 
  if isRange(b):
    checkSonsLen(b, 3)
    result = semBranchRange(c, t, b.sons[1], b.sons[2], covered)
  elif b.kind == nkRange:
    checkSonsLen(b, 2)
    result = semBranchRange(c, t, b.sons[0], b.sons[1], covered)
  else:
    result = fitNode(c, t.sons[0].typ, b)
    inc(covered)

proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int, 
                   covered: var BiggestInt) = 
  for i in countup(0, sonsLen(branch) - 2): 
    var b = branch.sons[i]
    if b.kind == nkRange:
      branch.sons[i] = b
    elif isRange(b):
      branch.sons[i] = semCaseBranchRange(c, t, b, covered)
    else:
      var r = semConstExpr(c, b)
      # for ``{}`` we want to trigger the type mismatch in ``fitNode``:
      if r.kind != nkCurly or len(r) == 0:
        checkMinSonsLen(t, 1)
        branch.sons[i] = skipConv(fitNode(c, t.sons[0].typ, r))
        inc(covered)
      else:
        # constant sets have special rules
        # first element is special and will overwrite: branch.sons[i]:
        branch.sons[i] = semCaseBranchSetElem(c, t, r[0], covered)
        # other elements have to be added to ``branch``
        for j in 1 .. <r.len:
          branch.add(semCaseBranchSetElem(c, t, r[j], covered))
          # caution! last son of branch must be the actions to execute:
          var L = branch.len
          swap(branch.sons[L-2], branch.sons[L-1])
    checkForOverlap(c, t, i, branchIndex)
    
proc semRecordNodeAux(c: PContext, n: PNode, check: var TIntSet, pos: var int,
                      father: PNode, rectype: PType)
proc semRecordCase(c: PContext, n: PNode, check: var TIntSet, pos: var int,
                   father: PNode, rectype: PType) =
  var a = copyNode(n)
  checkMinSonsLen(n, 2)
  semRecordNodeAux(c, n.sons[0], check, pos, a, rectype)
  if a.sons[0].kind != nkSym:
    internalError("semRecordCase: discriminant is no symbol")
    return
  incl(a.sons[0].sym.flags, sfDiscriminant)
  var covered: BiggestInt = 0
  var typ = skipTypes(a.sons[0].typ, abstractVar-{tyTypeDesc})
  if not isOrdinalType(typ):
    localError(n.info, errSelectorMustBeOrdinal)
  elif firstOrd(typ) < 0:
    localError(n.info, errOrdXMustNotBeNegative, a.sons[0].sym.name.s)
  elif lengthOrd(typ) > 0x00007FFF:
    localError(n.info, errLenXinvalid, a.sons[0].sym.name.s)
  var chckCovered = true
  for i in countup(1, sonsLen(n) - 1):
    var b = copyTree(n.sons[i])
    addSon(a, b)
    case n.sons[i].kind
    of nkOfBranch:
      checkMinSonsLen(b, 2)
      semCaseBranch(c, a, b, i, covered)
    of nkElse:
      chckCovered = false
      checkSonsLen(b, 1)
    else: illFormedAst(n)
    delSon(b, sonsLen(b) - 1)
    semRecordNodeAux(c, lastSon(n.sons[i]), check, pos, b, rectype)
  if chckCovered and (covered != lengthOrd(a.sons[0].typ)): 
    localError(a.info, errNotAllCasesCovered)
  addSon(father, a)

proc semRecordNodeAux(c: PContext, n: PNode, check: var TIntSet, pos: var int, 
                      father: PNode, rectype: PType) =
  if n == nil: return
  case n.kind
  of nkRecWhen:
    var branch: PNode = nil   # the branch to take
    for i in countup(0, sonsLen(n) - 1):
      var it = n.sons[i]
      if it == nil: illFormedAst(n)
      var idx = 1
      case it.kind
      of nkElifBranch:
        checkSonsLen(it, 2)
        if c.inGenericContext == 0:
          var e = semConstBoolExpr(c, it.sons[0])
          if e.kind != nkIntLit: internalError(e.info, "semRecordNodeAux")
          elif e.intVal != 0 and branch == nil: branch = it.sons[1]
        else:
          it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))
      of nkElse:
        checkSonsLen(it, 1)
        if branch == nil: branch = it.sons[0]
        idx = 0
      else: illFormedAst(n)
      if c.inGenericContext > 0:
        # use a new check intset here for each branch:
        var newCheck: TIntSet
        assign(newCheck, check)
        var newPos = pos
        var newf = newNodeI(nkRecList, n.info)
        semRecordNodeAux(c, it.sons[idx], newCheck, newPos, newf, rectype)
        it.sons[idx] = if newf.len == 1: newf[0] else: newf
    if c.inGenericContext > 0:
      addSon(father, n)
    elif branch != nil:
      semRecordNodeAux(c, branch, check, pos, father, rectype)
  of nkRecCase:
    semRecordCase(c, n, check, pos, father, rectype)
  of nkNilLit: 
    if father.kind != nkRecList: addSon(father, newNodeI(nkRecList, n.info))
  of nkRecList:
    # attempt to keep the nesting at a sane level:
    var a = if father.kind == nkRecList: father else: copyNode(n)
    for i in countup(0, sonsLen(n) - 1): 
      semRecordNodeAux(c, n.sons[i], check, pos, a, rectype)
    if a != father: addSon(father, a)
  of nkIdentDefs:
    checkMinSonsLen(n, 3)
    var length = sonsLen(n)
    var a: PNode
    if father.kind != nkRecList and length>=4: a = newNodeI(nkRecList, n.info)
    else: a = ast.emptyNode
    if n.sons[length-1].kind != nkEmpty: 
      localError(n.sons[length-1].info, errInitHereNotAllowed)
    var typ: PType
    if n.sons[length-2].kind == nkEmpty: 
      localError(n.info, errTypeExpected)
      typ = errorType(c)
    else:
      typ = semTypeNode(c, n.sons[length-2], nil)
      propagateToOwner(rectype, typ)
    let rec = rectype.sym
    for i in countup(0, sonsLen(n)-3):
      var f = semIdentWithPragma(c, skField, n.sons[i], {sfExported})
      suggestSym(n.sons[i], f)
      f.typ = typ
      f.position = pos
      if (rec != nil) and ({sfImportc, sfExportc} * rec.flags != {}) and
          (f.loc.r == nil): 
        f.loc.r = toRope(f.name.s)
        f.flags = f.flags + ({sfImportc, sfExportc} * rec.flags)
      inc(pos)
      if containsOrIncl(check, f.name.id):
        localError(n.sons[i].info, errAttemptToRedefine, f.name.s)
      if a.kind == nkEmpty: addSon(father, newSymNode(f))
      else: addSon(a, newSymNode(f))
    if a.kind != nkEmpty: addSon(father, a)
  of nkEmpty: discard
  else: illFormedAst(n)
  
proc addInheritedFieldsAux(c: PContext, check: var TIntSet, pos: var int, 
                           n: PNode) =
  case n.kind
  of nkRecCase:
    if (n.sons[0].kind != nkSym): internalError(n.info, "addInheritedFieldsAux")
    addInheritedFieldsAux(c, check, pos, n.sons[0])
    for i in countup(1, sonsLen(n) - 1):
      case n.sons[i].kind
      of nkOfBranch, nkElse:
        addInheritedFieldsAux(c, check, pos, lastSon(n.sons[i]))
      else: internalError(n.info, "addInheritedFieldsAux(record case branch)")
  of nkRecList:
    for i in countup(0, sonsLen(n) - 1):
      addInheritedFieldsAux(c, check, pos, n.sons[i])
  of nkSym:
    incl(check, n.sym.name.id)
    inc(pos)
  else: internalError(n.info, "addInheritedFieldsAux()")
  
proc addInheritedFields(c: PContext, check: var TIntSet, pos: var int, 
                        obj: PType) = 
  if (sonsLen(obj) > 0) and (obj.sons[0] != nil): 
    addInheritedFields(c, check, pos, obj.sons[0])
  addInheritedFieldsAux(c, check, pos, obj.n)

proc skipGenericInvokation(t: PType): PType {.inline.} = 
  result = t
  if result.kind == tyGenericInvokation:
    result = result.sons[0]
  if result.kind == tyGenericBody:
    result = lastSon(result)

proc semObjectNode(c: PContext, n: PNode, prev: PType): PType =
  if n.sonsLen == 0: return newConstraint(c, tyObject)
  var check = initIntSet()
  var pos = 0 
  var base: PType = nil
  # n.sons[0] contains the pragmas (if any). We process these later...
  checkSonsLen(n, 3)
  if n.sons[1].kind != nkEmpty: 
    base = skipTypes(semTypeNode(c, n.sons[1].sons[0], nil), skipPtrs)
    var concreteBase = skipGenericInvokation(base).skipTypes(skipPtrs)
    if concreteBase.kind == tyObject and tfFinal notin concreteBase.flags: 
      addInheritedFields(c, check, pos, concreteBase)
    else:
      if concreteBase.kind != tyError:
        localError(n.sons[1].info, errInheritanceOnlyWithNonFinalObjects)
      base = nil
  if n.kind != nkObjectTy: internalError(n.info, "semObjectNode")
  result = newOrPrevType(tyObject, prev, c)
  rawAddSon(result, base)
  result.n = newNodeI(nkRecList, n.info)
  semRecordNodeAux(c, n.sons[2], check, pos, result.n, result)
  if n.sons[0].kind != nkEmpty:
    # dummy symbol for `pragma`:
    var s = newSymS(skType, newIdentNode(getIdent("dummy"), n.info), c)
    s.typ = result
    pragma(c, s, n.sons[0], typePragmas)
  if base == nil and tfInheritable notin result.flags:
    incl(result.flags, tfFinal)

proc findEnforcedStaticType(t: PType): PType =
  # This handles types such as `static[T] and Foo`,
  # which are subset of `static[T]`, hence they could
  # be treated in the same way
  if t.kind == tyStatic: return t
  if t.kind == tyAnd:
    for s in t.sons:
      let t = findEnforcedStaticType(s)
      if t != nil: return t

proc addParamOrResult(c: PContext, param: PSym, kind: TSymKind) =
  template addDecl(x) =
    if sfGenSym notin x.flags: addDecl(c, x)

  if kind == skMacro:
    let staticType = findEnforcedStaticType(param.typ)
    if staticType != nil:
      var a = copySym(param)
      a.typ = staticType.base
      addDecl(a)
    elif param.typ.kind == tyTypeDesc:
      addDecl(param)
    else:
      # within a macro, every param has the type PNimrodNode!
      let nn = getSysSym"PNimrodNode"
      var a = copySym(param)
      a.typ = nn.typ
      addDecl(a)
  else:
    addDecl(param)

let typedescId = getIdent"typedesc"

template shouldHaveMeta(t) =
  internalAssert tfHasMeta in t.flags
  # result.lastSon.flags.incl tfHasMeta

proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
                   paramType: PType, paramName: string,
                   info: TLineInfo, anon = false): PType =
  if paramType == nil: return # (e.g. proc return type)

  proc addImplicitGenericImpl(typeClass: PType, typId: PIdent): PType =
    let finalTypId = if typId != nil: typId
                     else: getIdent(paramName & ":type")
    if genericParams == nil:
      # This happens with anonymous proc types appearing in signatures
      # XXX: we need to lift these earlier
      return
    # is this a bindOnce type class already present in the param list?
    for i in countup(0, genericParams.len - 1):
      if genericParams.sons[i].sym.name.id == finalTypId.id:
        return genericParams.sons[i].typ

    let owner = if typeClass.sym != nil: typeClass.sym
                else: getCurrOwner()
    var s = newSym(skType, finalTypId, owner, info)
    if typId == nil: s.flags.incl(sfAnon)
    s.linkTo(typeClass)
    typeClass.flags.incl tfImplicitTypeParam
    s.position = genericParams.len
    genericParams.addSon(newSymNode(s))
    result = typeClass
    addDecl(c, s)
 
  # XXX: There are codegen errors if this is turned into a nested proc
  template liftingWalk(typ: PType, anonFlag = false): expr =
    liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)
  #proc liftingWalk(paramType: PType, anon = false): PType =

  var paramTypId = if not anon and paramType.sym != nil: paramType.sym.name
                   else: nil

  template maybeLift(typ: PType): expr =
    let lifted = liftingWalk(typ)
    (if lifted != nil: lifted else: typ)

  template addImplicitGeneric(e: expr): expr =
    addImplicitGenericImpl(e, paramTypId)

  case paramType.kind:
  of tyAnything:
    result = addImplicitGeneric(newTypeS(tyGenericParam, c))
  
  of tyStatic:
    # proc(a: expr{string}, b: expr{nkLambda})
    # overload on compile time values and AST trees
    if paramType.n != nil: return # this is a concrete type
    if tfUnresolved in paramType.flags: return # already lifted
    let base = paramType.base.maybeLift
    if base.isMetaType and procKind == skMacro:
      localError(info, errMacroBodyDependsOnGenericTypes, paramName)
    result = addImplicitGeneric(c.newTypeWithSons(tyStatic, @[base]))
    result.flags.incl({tfHasStatic, tfUnresolved})
  
  of tyTypeDesc:
    if tfUnresolved notin paramType.flags:
      # naked typedescs are not bindOnce types
      if paramType.base.kind == tyNone and paramTypId != nil and
         paramTypId.id == typedescId.id: paramTypId = nil
      result = addImplicitGeneric(
        c.newTypeWithSons(tyTypeDesc, @[paramType.base]))
  
  of tyDistinct:
    if paramType.sonsLen == 1:
      # disable the bindOnce behavior for the type class
      result = liftingWalk(paramType.sons[0], true)
  
  of tySequence, tySet, tyArray, tyOpenArray,
     tyVar, tyPtr, tyRef, tyProc:
    # XXX: this is a bit strange, but proc(s: seq)
    # produces tySequence(tyGenericParam, tyNone).
    # This also seems to be true when creating aliases
    # like: type myseq = distinct seq.
    # Maybe there is another better place to associate
    # the seq type class with the seq identifier.
    if paramType.kind == tySequence and paramType.lastSon.kind == tyNone:
      let typ = c.newTypeWithSons(tyBuiltInTypeClass,
                                  @[newTypeS(paramType.kind, c)])
      result = addImplicitGeneric(typ)
    else:
      for i in 0 .. <paramType.sons.len:
        var lifted = liftingWalk(paramType.sons[i])
        if lifted != nil:
          paramType.sons[i] = lifted
          result = paramType
  
  of tyGenericBody:
    result = newTypeS(tyGenericInvokation, c)
    result.rawAddSon(paramType)
    for i in 0 .. paramType.sonsLen - 2:
      result.rawAddSon newTypeS(tyAnything, c)
      # result.rawAddSon(copyType(paramType.sons[i], getCurrOwner(), true))

    if paramType.lastSon.kind == tyUserTypeClass:
      result.kind = tyUserTypeClassInst
      result.rawAddSon paramType.lastSon
      return addImplicitGeneric(result)
   
    result = instGenericContainer(c, paramType.sym.info, result,
                                  allowMetaTypes = true)
    result = newTypeWithSons(c, tyCompositeTypeClass, @[paramType, result])
    result = addImplicitGeneric(result)

  of tyIter:
    if paramType.callConv == ccInline:
      if procKind notin {skTemplate, skMacro, skIterator}:
        localError(info, errInlineIteratorsAsProcParams)
      if paramType.len == 1:
        let lifted = liftingWalk(paramType.base)
        if lifted != nil: paramType.sons[0] = lifted
      result = addImplicitGeneric(paramType)

  of tyGenericInst:
    if paramType.lastSon.kind == tyUserTypeClass:
      var cp = copyType(paramType, getCurrOwner(), false)
      cp.kind = tyUserTypeClassInst
      return addImplicitGeneric(cp)

    for i in 1 .. (paramType.sons.len - 2):
      var lifted = liftingWalk(paramType.sons[i])
      if lifted != nil:
        paramType.sons[i] = lifted
        result = paramType
        result.lastSon.shouldHaveMeta

    let liftBody = liftingWalk(paramType.lastSon, true)
    if liftBody != nil:
      result = liftBody
      result.shouldHaveMeta
 
  of tyGenericInvokation:
    for i in 1 .. <paramType.sonsLen:
      let lifted = liftingWalk(paramType.sons[i])
      if lifted != nil: paramType.sons[i] = lifted

    let expanded = instGenericContainer(c, info, paramType,
                                        allowMetaTypes = true)
    result = liftingWalk(expanded, true)

  of tyUserTypeClass, tyBuiltInTypeClass, tyAnd, tyOr, tyNot:
    result = addImplicitGeneric(copyType(paramType, getCurrOwner(), true))
  
  of tyExpr:
    if procKind notin {skMacro, skTemplate}:
      result = addImplicitGeneric(newTypeS(tyAnything, c))
  
  of tyGenericParam:
    markUsed(genericParams, paramType.sym)
    if tfWildcard in paramType.flags:
      paramType.flags.excl tfWildcard
      paramType.sym.kind = skType
    
  else: discard

  # result = liftingWalk(paramType)

proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType =
  if n.kind == nkCurlyExpr:
    result = semTypeNode(c, n.sons[0], nil)
    constraint = semNodeKindConstraints(n)
  else:
    result = semTypeNode(c, n, nil)

proc semProcTypeNode(c: PContext, n, genericParams: PNode,
                     prev: PType, kind: TSymKind): PType =
  var
    res: PNode
    cl: TIntSet
  checkMinSonsLen(n, 1)
  result = newOrPrevType(tyProc, prev, c)
  result.callConv = lastOptionEntry(c).defaultCC
  result.n = newNodeI(nkFormalParams, n.info)
  if genericParams != nil and sonsLen(genericParams) == 0:
    cl = initIntSet()
  rawAddSon(result, nil) # return type
  # result.n[0] used to be `nkType`, but now it's `nkEffectList` because 
  # the effects are now stored in there too ... this is a bit hacky, but as
  # usual we desperately try to save memory:
  res = newNodeI(nkEffectList, n.info)
  addSon(result.n, res)
  var check = initIntSet()
  var counter = 0
  for i in countup(1, n.len - 1):
    var a = n.sons[i]
    if a.kind != nkIdentDefs: illFormedAst(a)
    checkMinSonsLen(a, 3)
    var
      typ: PType = nil
      def: PNode = nil
      constraint: PNode = nil
      length = sonsLen(a)
      hasType = a.sons[length-2].kind != nkEmpty
      hasDefault = a.sons[length-1].kind != nkEmpty
    if hasType:
      typ = semParamType(c, a.sons[length-2], constraint)

    if hasDefault:
      def = semExprWithType(c, a.sons[length-1]) 
      # check type compability between def.typ and typ:
      if typ == nil:
        typ = def.typ
      elif def != nil:
        # and def.typ != nil and def.typ.kind != tyNone:
        # example code that triggers it:
        # proc sort[T](cmp: proc(a, b: T): int = cmp)
        if not containsGenericType(typ):
          def = fitNode(c, typ, def)
    if not (hasType or hasDefault):
      let tdef = if kind in {skTemplate, skMacro}: tyExpr else: tyAnything
      typ = newTypeS(tdef, c)

    if skipTypes(typ, {tyGenericInst}).kind == tyEmpty: continue
    for j in countup(0, length-3): 
      var arg = newSymG(skParam, a.sons[j], c)
      let lifted = liftParamType(c, kind, genericParams, typ,
                                 arg.name.s, arg.info)
      let finalType = if lifted != nil: lifted else: typ.skipIntLit
      arg.typ = finalType
      arg.position = counter
      arg.constraint = constraint
      inc(counter)
      if def != nil and def.kind != nkEmpty: arg.ast = copyTree(def)
      if containsOrIncl(check, arg.name.id): 
        localError(a.sons[j].info, errAttemptToRedefine, arg.name.s)
      addSon(result.n, newSymNode(arg))
      rawAddSon(result, finalType)
      addParamOrResult(c, arg, kind)
      if gCmd == cmdPretty: checkDef(a.sons[j], arg)

  var r: PType
  if n.sons[0].kind != nkEmpty:
    r = semTypeNode(c, n.sons[0], nil)
  elif kind == skIterator:
    r = newTypeS(tyAnything, c)
  
  if r != nil:
    # turn explicit 'void' return type into 'nil' because the rest of the 
    # compiler only checks for 'nil':
    if skipTypes(r, {tyGenericInst}).kind != tyEmpty:
      if r.sym == nil or sfAnon notin r.sym.flags:
        let lifted = liftParamType(c, kind, genericParams, r, "result",
                                   n.sons[0].info)
        if lifted != nil: r = lifted
        r.flags.incl tfRetType
      r = skipIntLit(r)
      if kind == skIterator:
        # see tchainediterators
        # in cases like iterator foo(it: iterator): type(it)
        # we don't need to change the return type to iter[T]
        if not r.isInlineIterator: r = newTypeWithSons(c, tyIter, @[r])
      result.sons[0] = r
      res.typ = r

  if genericParams != nil:
    for n in genericParams:
      if tfWildcard in n.sym.typ.flags:
        n.sym.kind = skType
        n.sym.typ.flags.excl tfWildcard

proc semStmtListType(c: PContext, n: PNode, prev: PType): PType =
  checkMinSonsLen(n, 1)
  var length = sonsLen(n)
  for i in countup(0, length - 2):
    n.sons[i] = semStmt(c, n.sons[i])
  if length > 0:
    result = semTypeNode(c, n.sons[length - 1], prev)
    n.typ = result
    n.sons[length - 1].typ = result
  else:
    result = nil
  
proc semBlockType(c: PContext, n: PNode, prev: PType): PType = 
  inc(c.p.nestedBlockCounter)
  checkSonsLen(n, 2)
  openScope(c)
  if n.sons[0].kind notin {nkEmpty, nkSym}:
    addDecl(c, newSymS(skLabel, n.sons[0], c))
  result = semStmtListType(c, n.sons[1], prev)
  n.sons[1].typ = result
  n.typ = result
  closeScope(c)
  dec(c.p.nestedBlockCounter)

proc semGenericParamInInvokation(c: PContext, n: PNode): PType =
  result = semTypeNode(c, n, nil)

proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
  if s.typ == nil:
    localError(n.info, "cannot instantiate the '$1' $2" %
                       [s.name.s, ($s.kind).substr(2).toLower])
    return newOrPrevType(tyError, prev, c)
  
  var t = s.typ
  if t.kind == tyCompositeTypeClass and t.base.kind == tyGenericBody:
    t = t.base

  result = newOrPrevType(tyGenericInvokation, prev, c)
  addSonSkipIntLit(result, t)

  template addToResult(typ) =
    if typ.isNil:
      internalAssert false
      rawAddSon(result, typ)
    else: addSonSkipIntLit(result, typ)

  if t.kind == tyForward:
    for i in countup(1, sonsLen(n)-1):
      var elem = semGenericParamInInvokation(c, n.sons[i])
      addToResult(elem)
    return
  elif t.kind != tyGenericBody:
    #we likely got code of the form TypeA[TypeB] where TypeA is
    #not generic.
    localError(n.info, errNoGenericParamsAllowedForX, s.name.s)
    return newOrPrevType(tyError, prev, c)
  else:
    var m = newCandidate(c, t)
    matches(c, n, copyTree(n), m)
    
    if m.state != csMatch:
      var err = "cannot instantiate " & typeToString(t) & "\n" &
                "got: (" & describeArgs(c, n) & ")\n" &
                "but expected: (" & describeArgs(c, t.n, 0) & ")"
      localError(n.info, errGenerated, err)
      return newOrPrevType(tyError, prev, c)

    var isConcrete = true
 
    for i in 1 .. <m.call.len:
      let typ = m.call[i].typ.skipTypes({tyTypeDesc})
      if containsGenericType(typ): isConcrete = false
      addToResult(typ)
   
    if isConcrete:
      if s.ast == nil and s.typ.kind != tyCompositeTypeClass:
        # XXX: What kind of error is this? is it still relevant?
        localError(n.info, errCannotInstantiateX, s.name.s)
        result = newOrPrevType(tyError, prev, c)
      else:
        result = instGenericContainer(c, n.info, result,
                                      allowMetaTypes = false)

proc semTypeExpr(c: PContext, n: PNode): PType =
  var n = semExprWithType(c, n, {efDetermineType})
  if n.typ.kind == tyTypeDesc:
    result = n.typ.base
  else:
    localError(n.info, errTypeExpected, n.renderTree)

proc freshType(res, prev: PType): PType {.inline.} =
  if prev.isNil:
    result = copyType(res, res.owner, keepId=false)
  else:
    result = res

proc semTypeClass(c: PContext, n: PNode, prev: PType): PType =
  # if n.sonsLen == 0: return newConstraint(c, tyTypeClass)
  result = newOrPrevType(tyUserTypeClass, prev, c)
  result.n = n

  let
    pragmas = n[1]
    inherited = n[2]
    
  if inherited.kind != nkEmpty:
    for n in inherited.sons:
      let typ = semTypeNode(c, n, nil)
      result.sons.safeAdd(typ)

proc semProcTypeWithScope(c: PContext, n: PNode,
                        prev: PType, kind: TSymKind): PType =
  checkSonsLen(n, 2)
  openScope(c)
  result = semProcTypeNode(c, n.sons[0], nil, prev, kind)
  # dummy symbol for `pragma`:
  var s = newSymS(kind, newIdentNode(getIdent("dummy"), n.info), c)
  s.typ = result
  if n.sons[1].kind == nkEmpty or n.sons[1].len == 0:
    if result.callConv == ccDefault:
      result.callConv = ccClosure
      #Message(n.info, warnImplicitClosure, renderTree(n))
  else:
    pragma(c, s, n.sons[1], procTypePragmas)
    when useEffectSystem: setEffectsForProcType(result, n.sons[1])
  closeScope(c)

proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
  result = nil
  if gCmd == cmdIdeTools: suggestExpr(c, n)
  case n.kind
  of nkEmpty: discard
  of nkTypeOfExpr:
    # for ``type(countup(1,3))``, see ``tests/ttoseq``.
    checkSonsLen(n, 1)
    let typExpr = semExprWithType(c, n.sons[0], {efInTypeof})
    result = typExpr.typ.skipTypes({tyIter})
  of nkPar: 
    if sonsLen(n) == 1: result = semTypeNode(c, n.sons[0], prev)
    else:
      # XXX support anon tuple here
      localError(n.info, errTypeExpected)
      result = newOrPrevType(tyError, prev, c)
  of nkCallKinds:
    if isRange(n):
      result = semRangeAux(c, n, prev)
    elif n[0].kind == nkIdent:
      let op = n.sons[0].ident
      if op.id in {ord(wAnd), ord(wOr)} or op.s == "|":
        checkSonsLen(n, 3)
        var
          t1 = semTypeNode(c, n.sons[1], nil)
          t2 = semTypeNode(c, n.sons[2], nil)
        if t1 == nil:
          localError(n.sons[1].info, errTypeExpected)
          result = newOrPrevType(tyError, prev, c)
        elif t2 == nil:
          localError(n.sons[2].info, errTypeExpected)
          result = newOrPrevType(tyError, prev, c)
        else:
          result = if op.id == ord(wAnd): makeAndType(c, t1, t2)
                   else: makeOrType(c, t1, t2)
      elif op.id == ord(wNot):
        case n.len
        of 3:
          result = semTypeNode(c, n.sons[1], prev)
          if result.kind in NilableTypes and n.sons[2].kind == nkNilLit:
            result = freshType(result, prev)
            result.flags.incl(tfNotNil)
          else:
            localError(n.info, errGenerated, "invalid type")
        of 2:
          let negated = semTypeNode(c, n.sons[1], prev)
          result = makeNotType(c, negated)
        else:
          localError(n.info, errGenerated, "invalid type")
      else:
        result = semTypeExpr(c, n)
    else:
      result = semTypeExpr(c, n)
  of nkWhenStmt:
    var whenResult = semWhen(c, n, false)
    if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType
    result = semTypeNode(c, whenResult, prev)
  of nkBracketExpr:
    checkMinSonsLen(n, 2)
    var s = semTypeIdent(c, n.sons[0])
    case s.magic
    of mArray: result = semArray(c, n, prev)
    of mOpenArray: result = semContainer(c, n, tyOpenArray, "openarray", prev)
    of mRange: result = semRange(c, n, prev)
    of mSet: result = semSet(c, n, prev)
    of mOrdinal: result = semOrdinal(c, n, prev)
    of mSeq: result = semContainer(c, n, tySequence, "seq", prev)
    of mVarargs: result = semVarargs(c, n, prev)
    of mTypeDesc: result = makeTypeDesc(c, semTypeNode(c, n[1], nil))
    of mExpr:
      result = semTypeNode(c, n.sons[0], nil)
      if result != nil:
        result = copyType(result, getCurrOwner(), false)
        for i in countup(1, n.len - 1):
          result.rawAddSon(semTypeNode(c, n.sons[i], nil))
    else: result = semGeneric(c, n, s, prev)
  of nkDotExpr:
    var typeExpr = semExpr(c, n)
    if typeExpr.typ.kind != tyTypeDesc:
      localError(n.info, errTypeExpected)
      return errorType(c)
    result = typeExpr.typ.base
    if result.isMetaType:
      var toBind = initIntSet()
      var preprocessed = semGenericStmt(c, n, {}, toBind)
      return makeTypeFromExpr(c, preprocessed)
  of nkIdent, nkAccQuoted:
    var s = semTypeIdent(c, n)
    if s.typ == nil: 
      if s.kind != skError: localError(n.info, errTypeExpected)
      result = newOrPrevType(tyError, prev, c)
    elif s.kind == skParam and s.typ.kind == tyTypeDesc:
      internalAssert s.typ.base.kind != tyNone and prev == nil
      result = s.typ.base
    elif prev == nil:
      result = s.typ
    else: 
      assignType(prev, s.typ)
      # bugfix: keep the fresh id for aliases to integral types:
      if s.typ.kind notin {tyBool, tyChar, tyInt..tyInt64, tyFloat..tyFloat128,
                           tyUInt..tyUInt64}: 
        prev.id = s.typ.id
      result = prev
  of nkSym:
    if n.sym.kind == skType and n.sym.typ != nil:
      var t = n.sym.typ
      if prev == nil: 
        result = t
      else: 
        assignType(prev, t)
        result = prev
      markUsed(n, n.sym)
    else:
      if n.sym.kind != skError: localError(n.info, errTypeExpected)
      result = newOrPrevType(tyError, prev, c)
  of nkObjectTy: result = semObjectNode(c, n, prev)
  of nkTupleTy: result = semTuple(c, n, prev)
  of nkTypeClassTy: result = semTypeClass(c, n, prev)
  of nkRefTy: result = semAnyRef(c, n, tyRef, prev)
  of nkPtrTy: result = semAnyRef(c, n, tyPtr, prev)
  of nkVarTy: result = semVarType(c, n, prev)
  of nkDistinctTy: result = semDistinct(c, n, prev)
  of nkStaticTy:
    result = newOrPrevType(tyStatic, prev, c)
    var base = semTypeNode(c, n.sons[0], nil)
    result.rawAddSon(base)
    result.flags.incl tfHasStatic
  of nkIteratorTy:
    if n.sonsLen == 0:
      result = newConstraint(c, tyIter)
    else:
      result = semProcTypeWithScope(c, n, prev, skClosureIterator)
      if n.lastSon.kind == nkPragma and hasPragma(n.lastSon, wInline):
        result.kind = tyIter
        result.callConv = ccInline
      else:
        result.flags.incl(tfIterator)
        result.callConv = ccClosure
  of nkProcTy:
    if n.sonsLen == 0:
      result = newConstraint(c, tyProc)
    else:
      result = semProcTypeWithScope(c, n, prev, skProc)
  of nkEnumTy: result = semEnum(c, n, prev)
  of nkType: result = n.typ
  of nkStmtListType: result = semStmtListType(c, n, prev)
  of nkBlockType: result = semBlockType(c, n, prev)
  of nkSharedTy:
    checkSonsLen(n, 1)
    result = semTypeNode(c, n.sons[0], prev)
    result = freshType(result, prev)
    result.flags.incl(tfShared)
  else:
    localError(n.info, errTypeExpected)
    result = newOrPrevType(tyError, prev, c)
  
proc setMagicType(m: PSym, kind: TTypeKind, size: int) = 
  m.typ.kind = kind
  m.typ.align = size
  m.typ.size = size
  
proc processMagicType(c: PContext, m: PSym) = 
  case m.magic
  of mInt: setMagicType(m, tyInt, intSize)
  of mInt8: setMagicType(m, tyInt8, 1)
  of mInt16: setMagicType(m, tyInt16, 2)
  of mInt32: setMagicType(m, tyInt32, 4)
  of mInt64: setMagicType(m, tyInt64, 8)
  of mUInt: setMagicType(m, tyUInt, intSize)
  of mUInt8: setMagicType(m, tyUInt8, 1)
  of mUInt16: setMagicType(m, tyUInt16, 2)
  of mUInt32: setMagicType(m, tyUInt32, 4)
  of mUInt64: setMagicType(m, tyUInt64, 8)
  of mFloat: setMagicType(m, tyFloat, floatSize)
  of mFloat32: setMagicType(m, tyFloat32, 4)
  of mFloat64: setMagicType(m, tyFloat64, 8)
  of mFloat128: setMagicType(m, tyFloat128, 16)
  of mBool: setMagicType(m, tyBool, 1)
  of mChar: setMagicType(m, tyChar, 1)
  of mString: 
    setMagicType(m, tyString, ptrSize)
    rawAddSon(m.typ, getSysType(tyChar))
  of mCstring: 
    setMagicType(m, tyCString, ptrSize)
    rawAddSon(m.typ, getSysType(tyChar))
  of mPointer: setMagicType(m, tyPointer, ptrSize)
  of mEmptySet: 
    setMagicType(m, tySet, 1)
    rawAddSon(m.typ, newTypeS(tyEmpty, c))
  of mIntSetBaseType: setMagicType(m, tyRange, intSize)
  of mNil: setMagicType(m, tyNil, ptrSize)
  of mExpr: setMagicType(m, tyExpr, 0)
  of mStmt: setMagicType(m, tyStmt, 0)
  of mTypeDesc: 
    setMagicType(m, tyTypeDesc, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mVoidType: setMagicType(m, tyEmpty, 0)
  of mArray:
    setMagicType(m, tyArray, 0)
  of mOpenArray:
    setMagicType(m, tyOpenArray, 0)
  of mVarargs:
    setMagicType(m, tyVarargs, 0)
  of mRange:
    setMagicType(m, tyRange, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mSet:
    setMagicType(m, tySet, 0) 
  of mSeq: 
    setMagicType(m, tySequence, 0)
  of mOrdinal:
    setMagicType(m, tyOrdinal, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mPNimrodNode: discard
  else: localError(m.info, errTypeExpected)
  
proc semGenericConstraints(c: PContext, x: PType): PType =
  result = newTypeWithSons(c, tyGenericParam, @[x])

proc semGenericParamList(c: PContext, n: PNode, father: PType = nil): PNode = 
  result = copyNode(n)
  if n.kind != nkGenericParams: 
    illFormedAst(n)
    return
  for i in countup(0, sonsLen(n)-1):
    var a = n.sons[i]
    if a.kind != nkIdentDefs: illFormedAst(n)
    let L = a.len
    var def = a{-1}
    let constraint = a{-2}
    var typ: PType
    
    if constraint.kind != nkEmpty:
      typ = semTypeNode(c, constraint, nil)
      if typ.kind != tyStatic or typ.len == 0:
        if typ.kind == tyTypeDesc:
          if typ.sons[0].kind == tyNone:
            typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
        else:
          typ = semGenericConstraints(c, typ)
    
    if def.kind != nkEmpty:
      def = semConstExpr(c, def)
      if typ == nil:
        if def.typ.kind != tyTypeDesc:
          typ = newTypeWithSons(c, tyStatic, @[def.typ])
      else:
        if not containsGenericType(def.typ):
          def = fitNode(c, typ, def)
    
    if typ == nil:
      typ = newTypeS(tyGenericParam, c)
      if father == nil: typ.flags.incl tfWildcard

    typ.flags.incl tfGenericTypeParam

    for j in countup(0, L-3):
      let finalType = if j == 0: typ
                      else: copyType(typ, typ.owner, false)
                      # it's important the we create an unique
                      # type for each generic param. the index
                      # of the parameter will be stored in the
                      # attached symbol.
      var s = if finalType.kind == tyStatic or tfWildcard in typ.flags:
          newSymG(skGenericParam, a.sons[j], c).linkTo(finalType)
        else:
          newSymG(skType, a.sons[j], c).linkTo(finalType)
      if def.kind != nkEmpty: s.ast = def
      if father != nil: addSonSkipIntLit(father, s.typ)
      s.position = result.len
      addSon(result, newSymNode(s))
      if sfGenSym notin s.flags: addDecl(c, s)