summary refs log tree commit diff stats
path: root/tests/js/tstrconcat.nim
Commit message (Collapse)AuthorAgeFilesLines
* Fix concat behaviour for uninitialized strings (#8950)LemonBoy2018-09-111-0/+5
hor Araq <rumpf_a@web.de> 2012-01-02 23:07:35 +0100 committer Araq <rumpf_a@web.de> 2012-01-02 23:07:35 +0100 year 2012 for most copyright headers' href='/ahoang/Nim/commit/compiler/semtypes.nim?h=devel&id=4f1b89c30cff4b91d5656c856f05be4604adaeaa'>4f1b89c30 ^
e25474154 ^



6c2050912 ^
e25474154 ^
e5bd3b5b9 ^
e25474154 ^
ce859d8c1 ^
78c0ac540 ^
ce859d8c1 ^



8cf564362 ^
a73ae3e06 ^
404e91ce4 ^
ce859d8c1 ^




ce859d8c1 ^













b870744d5 ^
5536f7499 ^
e25474154 ^
b870744d5 ^
e25474154 ^

cce8d3da2 ^
064f29621 ^
b870744d5 ^
1d02f2ea5 ^
bf592c4e9 ^
226595515 ^
064f29621 ^

ed1d41c51 ^

1b4116702 ^

23ef565a3 ^
e25474154 ^


5db0bb744 ^
e25474154 ^



ce859d8c1 ^
7e747d11c ^

b870744d5 ^
7e747d11c ^
afbcd1b33 ^
049de0ef6 ^
580d85d23 ^
f8266c737 ^
81b7f9108 ^
23ef565a3 ^
7e747d11c ^


b870744d5 ^
7e747d11c ^

5db0bb744 ^
7e747d11c ^
c5ad4c10c ^
7e747d11c ^
5db0bb744 ^
7e747d11c ^
1a8c6fb49 ^
b870744d5 ^

7e747d11c ^

7e9442084 ^
7e747d11c ^
a73ae3e06 ^
7e747d11c ^
3b5a50469 ^
b595fc834 ^
ce859d8c1 ^
b595fc834 ^
ce859d8c1 ^
7e9442084 ^
1a8c6fb49 ^


6bdf7fdbc ^
a73ae3e06 ^
afbcd1b33 ^
3b5a50469 ^
5131b3cea ^

b870744d5 ^
7e747d11c ^
b595fc834 ^
1a8c6fb49 ^
e25474154 ^
b870744d5 ^
7e747d11c ^
b870744d5 ^
7e747d11c ^
5db0bb744 ^
c5ad4c10c ^
7e747d11c ^

5db0bb744 ^
20cb567bf ^
ce859d8c1 ^
e25474154 ^

5db0bb744 ^
16bc546ae ^

5db0bb744 ^
23ef565a3 ^
580d85d23 ^
8c42f5be0 ^
8161b0289 ^
5db0bb744 ^
800cb006e ^
6e8ed8c6f ^
fd4ef6ae8 ^
a273ea70e ^
5ebd1248d ^
a273ea70e ^

81b7f9108 ^
539fc5d58 ^
8cf564362 ^




979148e86 ^
fb80d2ff8 ^
1b570f2b1 ^

8241e5502 ^
e25474154 ^
b870744d5 ^
e25474154 ^
7e747d11c ^

226595515 ^
e4081a720 ^
cdd459dd6 ^
f2ddd9953 ^
a73ae3e06 ^
a4e2b0c15 ^
ce859d8c1 ^
b595fc834 ^
ce859d8c1 ^
226595515 ^
b870744d5 ^
abe0725ab ^
7e747d11c ^

afb0d2e14 ^
ce859d8c1 ^
226595515 ^
b870744d5 ^
ce859d8c1 ^
226595515 ^
064f29621 ^
abe0725ab ^




8805829d7 ^

7e747d11c ^

226595515 ^
7e747d11c ^

8805829d7 ^
ce859d8c1 ^
226595515 ^
4196757de ^
0d23419e6 ^
7e747d11c ^
0d23419e6 ^

6278b5d89 ^


b870744d5 ^
ce859d8c1 ^
7e747d11c ^
226595515 ^
064f29621 ^
0d23419e6 ^
8a0509b3b ^
a895bbf71 ^










141abb7b7 ^


ebdff1c7d ^
bbb6d2c69 ^
141abb7b7 ^
bbb6d2c69 ^
141abb7b7 ^




a895bbf71 ^












b870744d5 ^
8a0509b3b ^

a895bbf71 ^
8a0509b3b ^
b870744d5 ^
45a345e93 ^
438703f59 ^
ce859d8c1 ^
e25474154 ^

4c5351a1e ^

226595515 ^
4c5351a1e ^
45a345e93 ^
ce859d8c1 ^
b870744d5 ^
9fb075557 ^


b870744d5 ^
9fb075557 ^
2a1acbd1a ^
226595515 ^
9fb075557 ^
7fdf29937 ^




a273ea70e ^

7205c3ebe ^
90ed904c4 ^
ea91cfb30 ^
7fdf29937 ^
ce859d8c1 ^
b870744d5 ^
45a345e93 ^
5bcf8bcb5 ^
7e747d11c ^
0f2c4be12 ^
45a345e93 ^
7e747d11c ^
b870744d5 ^
9f3d1b129 ^

def623490 ^
45a345e93 ^
ce859d8c1 ^
45a345e93 ^
4c5351a1e ^
e25474154 ^
b595fc834 ^
e25474154 ^
7e747d11c ^
23ef565a3 ^

f89ba2c95 ^











b595fc834 ^
40ec7be45 ^
ce859d8c1 ^
2f17be9c2 ^
7e747d11c ^
79aaf213d ^
b595fc834 ^
ce859d8c1 ^
79aaf213d ^
b595fc834 ^
da36a847a ^
7fdf29937 ^

40b5c4c4c ^

da36a847a ^




651c0e45d ^
86af2f7b5 ^
651c0e45d ^
da36a847a ^



aa6f9d490 ^
12f7b96be ^
a73ae3e06 ^
da36a847a ^
a541be893 ^
5bcf8bcb5 ^
d5a0a5dff ^
a73ae3e06 ^
da36a847a ^










b870744d5 ^
da36a847a ^


ce859d8c1 ^
da36a847a ^
b870744d5 ^
da36a847a ^
7e747d11c ^
e25474154 ^
2d196442f ^

e4081a720 ^
2d196442f ^
5c5e54d3a ^


a73ae3e06 ^
2d196442f ^
7e747d11c ^
2d196442f ^
7e747d11c ^
2d196442f ^

a6226d945 ^


226595515 ^
b870744d5 ^
ce859d8c1 ^
79aaf213d ^
b870744d5 ^
ceadf54d7 ^








b870744d5 ^
e25474154 ^
7e747d11c ^

b870744d5 ^

a73ae3e06 ^
226595515 ^
b595fc834 ^
ce859d8c1 ^
79aaf213d ^
dfb5cb3c5 ^
dba25d262 ^
7e747d11c ^
ce859d8c1 ^
dba25d262 ^
47335aab4 ^
a895bbf71 ^

dba25d262 ^
13cdad881 ^
064f29621 ^
e25474154 ^

ade67f1ab ^
5b28d0820 ^
7e747d11c ^

ce859d8c1 ^

f89ba2c95 ^

141abb7b7 ^
f89ba2c95 ^
7e747d11c ^
f89ba2c95 ^



b595fc834 ^
ce859d8c1 ^
b595fc834 ^
7e747d11c ^

e25474154 ^


b870744d5 ^
7e747d11c ^
b595fc834 ^
f89ba2c95 ^


c814c4d99 ^
f89ba2c95 ^
226595515 ^
800cb006e ^
6e8ed8c6f ^
e92ccff57 ^
97c3b113a ^


b870744d5 ^

e25474154 ^
b870744d5 ^
7e747d11c ^
5e15dec17 ^

7e747d11c ^

b870744d5 ^
5131b3cea ^

88c99ad37 ^
7e747d11c ^
88c99ad37 ^
7e747d11c ^
5131b3cea ^
ce859d8c1 ^
5131b3cea ^
5e15dec17 ^
b870744d5 ^
18cbd5160 ^


ce859d8c1 ^
7e747d11c ^
e25474154 ^
18cbd5160 ^
e25474154 ^
bd705a517 ^
7e747d11c ^



438703f59 ^
7fcbdc6d4 ^
e25474154 ^
18cbd5160 ^
d5a5c2291 ^

7e747d11c ^

d5a5c2291 ^
7e747d11c ^
ce859d8c1 ^
b870744d5 ^
7d5d9f770 ^
ce859d8c1 ^
a79acb58f ^

cbd9fee22 ^

7e747d11c ^

b870744d5 ^
a79acb58f ^


ce859d8c1 ^
afbcd1b33 ^
e25474154 ^
b870744d5 ^
7d5d9f770 ^
ce859d8c1 ^
7e747d11c ^
8b6f9ef5e ^
b870744d5 ^
7d5d9f770 ^
8b6f9ef5e ^
ce859d8c1 ^
7e747d11c ^
8b6f9ef5e ^
ce859d8c1 ^
7e747d11c ^
8b6f9ef5e ^
7e747d11c ^
8b6f9ef5e ^

b870744d5 ^
7d5d9f770 ^
7e747d11c ^
e4515f304 ^
7e747d11c ^
65970efd9 ^
7e747d11c ^
65970efd9 ^
7e747d11c ^
8dda362fa ^
f9d7e8db2 ^
cfff454cf ^


7e747d11c ^
79384ea72 ^


7e747d11c ^
ce859d8c1 ^
7e747d11c ^
217a2cf09 ^
a95b6391f ^


7e747d11c ^
8b6f9ef5e ^

e4515f304 ^
a4e2b0c15 ^
e4515f304 ^
7e747d11c ^

e4515f304 ^
8b6f9ef5e ^
7e747d11c ^
8b6f9ef5e ^

7e747d11c ^
e4515f304 ^


7e747d11c ^
d5a5c2291 ^
b870744d5 ^
7d5d9f770 ^
1f1d7b108 ^

7e747d11c ^
1f1d7b108 ^
7d5d9f770 ^









afbcd1b33 ^
1f1d7b108 ^
594674797 ^
7a5b614ed ^
43a0dd42b ^
3c38edf56 ^














2e7c9eb6a ^
3c38edf56 ^












2e7c9eb6a ^

3c38edf56 ^

2e7c9eb6a ^

3c38edf56 ^









43a0dd42b ^
7e747d11c ^
3c38edf56 ^


43a0dd42b ^
594674797 ^
23ef565a3 ^
7ad5cab17 ^
ce859d8c1 ^
7e747d11c ^

ce859d8c1 ^
b0c11d3ef ^
7e747d11c ^
4ab6887a6 ^
1f1d7b108 ^
7e747d11c ^
90ed904c4 ^
1f1d7b108 ^
90ed904c4 ^
1f1d7b108 ^
191c38879 ^
1f1d7b108 ^
90ed904c4 ^
7e747d11c ^
90ed904c4 ^
07eca7dec ^
7e747d11c ^
07eca7dec ^
191c38879 ^
cf5c3f240 ^
7e747d11c ^
81125e202 ^

7e747d11c ^
81125e202 ^
7e747d11c ^



23ef565a3 ^
ce859d8c1 ^
e25474154 ^
23ef565a3 ^
ce859d8c1 ^
7e747d11c ^
83547ec76 ^
1f1d7b108 ^
ce859d8c1 ^
7e747d11c ^


3c38edf56 ^


43a0dd42b ^

7e747d11c ^
e25474154 ^
b870744d5 ^
46bfb590c ^
72651de71 ^
e25474154 ^
72651de71 ^
e98efdcc5 ^
72651de71 ^
e98efdcc5 ^

ce859d8c1 ^
72651de71 ^
e25474154 ^
72651de71 ^
ce859d8c1 ^
b731e6ef1 ^
7e747d11c ^
15f54728e ^
7e747d11c ^
72651de71 ^
7e747d11c ^
72651de71 ^
ce859d8c1 ^
7e747d11c ^
72651de71 ^
ce859d8c1 ^
b731e6ef1 ^
72651de71 ^
4d683fc68 ^
72651de71 ^

46bfb590c ^
7e747d11c ^
b731e6ef1 ^
e98efdcc5 ^
72651de71 ^
46bfb590c ^
4ab6887a6 ^

72651de71 ^
e25474154 ^
b870744d5 ^
7e747d11c ^
23ef565a3 ^
e25474154 ^
5b28d0820 ^
7e747d11c ^
46bfb590c ^
7e747d11c ^
72651de71 ^
ce859d8c1 ^
72651de71 ^
7e747d11c ^
2a7fc84c8 ^
b595fc834 ^
f89ba2c95 ^

141abb7b7 ^
f89ba2c95 ^

ce859d8c1 ^
b595fc834 ^

7e747d11c ^
f89ba2c95 ^



23ef565a3 ^
cbd059aa8 ^

7e747d11c ^



5b3571c9a ^
7e747d11c ^
796498525 ^
e25474154 ^

73cc029fe ^
cbd059aa8 ^

ca1f3f36b ^
39049e151 ^
13e659cfe ^
e25474154 ^
92b8fac94 ^
5b3571c9a ^
f89ba2c95 ^


c814c4d99 ^
f89ba2c95 ^

800cb006e ^
6e8ed8c6f ^
7e747d11c ^
0ff1190fe ^
ebb15505d ^

0ff1190fe ^

02b78d3f9 ^
7e747d11c ^
4ab6887a6 ^


ce859d8c1 ^
b870744d5 ^

23ef565a3 ^
e25474154 ^
23ef565a3 ^
7e747d11c ^



23ef565a3 ^
7e747d11c ^
ce859d8c1 ^
e54546bcf ^
51bd442b8 ^
7e747d11c ^
23ef565a3 ^
92b8fac94 ^
e25474154 ^
ce859d8c1 ^
e25474154 ^
b870744d5 ^
dc669155e ^
c95f6f117 ^
7e747d11c ^
ea409fb15 ^
dc669155e ^

28a116a47 ^











fd10866ae ^
39ad9a69a ^
ed1d41c51 ^
3c773c189 ^
ade67f1ab ^
b870744d5 ^
3a13706d7 ^
7e747d11c ^
ce859d8c1 ^
7e747d11c ^

3a13706d7 ^
a1b937ab3 ^
ce859d8c1 ^
dc669155e ^
cdc6529cc ^
ebb15505d ^





0ff1190fe ^
a6682de00 ^


28a116a47 ^


a1b937ab3 ^

7e747d11c ^
a71654323 ^

a1b937ab3 ^
3a13706d7 ^
ce859d8c1 ^
e25474154 ^
3a13706d7 ^
39ad9a69a ^
f31d1fa98 ^
39ad9a69a ^
0baa1d995 ^



28a116a47 ^


7e747d11c ^

cb79bf9f1 ^
40ec7be45 ^
cb79bf9f1 ^
7e747d11c ^
d17caa86e ^

ce9a4ed12 ^

027f30610 ^
f31d1fa98 ^







5536f7499 ^
39ad9a69a ^
f31d1fa98 ^




faedb14a1 ^
f31d1fa98 ^

b0684ec42 ^
f31d1fa98 ^
7e747d11c ^
f31d1fa98 ^
672dc5cd8 ^
f31d1fa98 ^



b0684ec42 ^

8362d92e4 ^
f31d1fa98 ^
4c7313931 ^
226595515 ^
f31d1fa98 ^
4c7313931 ^
226595515 ^

f31d1fa98 ^

672dc5cd8 ^
b0684ec42 ^








8362d92e4 ^

b0684ec42 ^
4533e894a ^
5fb40af57 ^
f31d1fa98 ^
4b7655fd1 ^



f94ec363a ^
4b7655fd1 ^





be1154106 ^
5aa486cf1 ^
4b7655fd1 ^

20b011de1 ^
4b7655fd1 ^
5f6131cbd ^
cb45527e3 ^

5aa486cf1 ^
1e6aef62b ^
ce859d8c1 ^
20b011de1 ^
5aa486cf1 ^
5f6131cbd ^
46e9ff613 ^
e87470597 ^




be1154106 ^
72291875b ^
ce859d8c1 ^
72291875b ^

97d77bf37 ^









7e747d11c ^


97d77bf37 ^


20b011de1 ^
97d77bf37 ^




7e747d11c ^
97d77bf37 ^


dfb5cb3c5 ^


8e0941576 ^

0c41fd0f5 ^
dfb5cb3c5 ^
dfb5cb3c5 ^



6278b5d89 ^
027f30610 ^
97d77bf37 ^
b870744d5 ^
027f30610 ^
fb27357b6 ^
e12946691 ^
fb27357b6 ^
f0953db3b ^
0c41fd0f5 ^


e6b0b7ecc ^
ce859d8c1 ^
97d77bf37 ^

2c98b4943 ^
b870744d5 ^
22dc76a36 ^
dfb5cb3c5 ^
56d75bd23 ^
cda92048b ^
4fd79f5b4 ^

40ec7be45 ^

bf592c4e9 ^

97d77bf37 ^
b870744d5 ^
bc2eb0ea9 ^
ed1d41c51 ^
dfb5cb3c5 ^
0c41fd0f5 ^
b870744d5 ^
0286a0879 ^






338602a40 ^
0c41fd0f5 ^
509d6e923 ^
8e0941576 ^
e4081a720 ^
dfb5cb3c5 ^
d912d1837 ^
dfb5cb3c5 ^



d912d1837 ^
66a255652 ^

97d77bf37 ^
dfb5cb3c5 ^
7e747d11c ^

ce859d8c1 ^
7e747d11c ^
dfb5cb3c5 ^
7e747d11c ^
dfb5cb3c5 ^
b870744d5 ^
bc2eb0ea9 ^
c95f6f117 ^
a27eb5153 ^
b870744d5 ^
7e747d11c ^


cbf66e99a ^

247af96b0 ^

b870744d5 ^
ee1b0d8c6 ^


97d77bf37 ^
b870744d5 ^
e97d640ce ^
a27eb5153 ^
e97d640ce ^

7e747d11c ^
97d77bf37 ^
085b339b8 ^
dfb5cb3c5 ^
a6a18be08 ^
73a8b950c ^
8241e5502 ^

a6a18be08 ^
97d77bf37 ^
a6a18be08 ^
7e747d11c ^

dfb5cb3c5 ^
7e747d11c ^
dfb5cb3c5 ^
72291875b ^
de3b7cd41 ^
0c41fd0f5 ^
66a255652 ^

f673fbd91 ^

b870744d5 ^
c95f6f117 ^
7e747d11c ^
b044523c5 ^
7e747d11c ^

66e0f0e19 ^
0b7321651 ^
45759b5e9 ^
0b7321651 ^




113a1c450 ^

0c41fd0f5 ^
754e2ef1d ^
bf4ce87e5 ^
6278b5d89 ^
97d77bf37 ^
73a8b950c ^
97d77bf37 ^
b870744d5 ^
1d02f2ea5 ^
81ddc6778 ^
35d96d874 ^
37b199d34 ^

796aafe7e ^
b870744d5 ^
73c6efdf6 ^
251c44ff0 ^
7171ae62c ^
e406e2873 ^
7171ae62c ^
7e747d11c ^
825e08b04 ^





7171ae62c ^


b5f195758 ^




b870744d5 ^
b5f195758 ^

7e747d11c ^
b5f195758 ^
e8f4586c6 ^
169d3fda0 ^
e8f4586c6 ^


37b199d34 ^
61c5c92f0 ^


ce859d8c1 ^
b5f195758 ^
ade67f1ab ^
6c2050912 ^
5bcf8bcb5 ^
7e747d11c ^

b56f43c39 ^





ce859d8c1 ^
5bcf8bcb5 ^
ce859d8c1 ^
70f5236d4 ^

7171ae62c ^

7e747d11c ^

5bcf8bcb5 ^
70f5236d4 ^
7e747d11c ^
730b1b2b8 ^
e406e2873 ^
13e659cfe ^
18e2ea787 ^
dfb5cb3c5 ^
e406e2873 ^
70f5236d4 ^
5bcf8bcb5 ^
4898b054c ^









5bcf8bcb5 ^
ac982d8a9 ^
ea2f2775a ^
ac982d8a9 ^
5bcf8bcb5 ^




ac982d8a9 ^
31651ecd6 ^

5bcf8bcb5 ^
dd99fe61c ^
e25474154 ^
6e4ea4047 ^
2cea52ee7 ^
6e4ea4047 ^
16b594b53 ^
e12946691 ^





16b594b53 ^
bf592c4e9 ^
16b594b53 ^
5bcf8bcb5 ^

e424e13bd ^

fd62116f6 ^
5bcf8bcb5 ^
2589d69ed ^
e719f211c ^


5bcf8bcb5 ^
61c5c92f0 ^
ce859d8c1 ^
38dee2095 ^
b92fd3028 ^
e4081a720 ^
38dee2095 ^
5bcf8bcb5 ^
7e747d11c ^
2cea52ee7 ^
ca82b4ea1 ^
ecf9efa39 ^


50b6f6996 ^

38dee2095 ^



037715285 ^
38dee2095 ^
ecf9efa39 ^


dfb5cb3c5 ^
ecf9efa39 ^
226595515 ^
251c44ff0 ^
e25474154 ^
7171ae62c ^
e25474154 ^
5bcf8bcb5 ^

7e747d11c ^
049de0ef6 ^
be1154106 ^
800cb006e ^
6e8ed8c6f ^
16bc546ae ^
b731e6ef1 ^
518b79449 ^
7e747d11c ^

b870744d5 ^
f94ec363a ^
dadc97d91 ^
f94ec363a ^

7e747d11c ^
f94ec363a ^



4c7313931 ^
f94ec363a ^

518b79449 ^
b870744d5 ^
00da785f5 ^
e4081a720 ^
b92fd3028 ^
7e747d11c ^
8922063bd ^
e9619d727 ^
8922063bd ^
5917c2d5b ^
ce148e71e ^


8922063bd ^
e9619d727 ^

7e747d11c ^
fd80a5154 ^




226595515 ^
e9619d727 ^

d447c0fe3 ^
e9619d727 ^
217e0ab6e ^
217e0ab6e ^
7e747d11c ^
14468e233 ^
c1ad75fa5 ^
14468e233 ^



b5f195758 ^
56d75bd23 ^
ea2f2775a ^
37b199d34 ^
1be0022e7 ^


f0953db3b ^
796aafe7e ^
37b199d34 ^

12bac28d2 ^
ce859d8c1 ^
7e747d11c ^



e25474154 ^
7e747d11c ^
12bac28d2 ^
e25474154 ^
b870744d5 ^

92b8fac94 ^
ef2998778 ^

ce859d8c1 ^
9a6f47ae6 ^
7e747d11c ^



e25474154 ^
9a6f47ae6 ^
ef2998778 ^
92b8fac94 ^
e25474154 ^
c95f6f117 ^
fd62116f6 ^
0b7321651 ^
fd62116f6 ^
28a116a47 ^
ebb15505d ^



7e747d11c ^
ebb15505d ^
28a116a47 ^
0ff1190fe ^
ce859d8c1 ^
0ff1190fe ^
ebb15505d ^
0ff1190fe ^
28a116a47 ^

0ff1190fe ^

ce859d8c1 ^
0ff1190fe ^


4b7655fd1 ^
b3da0ab88 ^
ce859d8c1 ^
faedb14a1 ^
b3da0ab88 ^
b870744d5 ^
e057b1d83 ^

4b7655fd1 ^

c95f6f117 ^
226595515 ^
3f71b7f1f ^
f760bc243 ^

ce859d8c1 ^
f760bc243 ^
226595515 ^
f760bc243 ^
4b7655fd1 ^
7e747d11c ^

f760bc243 ^
4b7655fd1 ^

765f27462 ^

ce859d8c1 ^
51076a0be ^
f760bc243 ^
4b7655fd1 ^
b0b716fca ^
f760bc243 ^
b870744d5 ^
a6006e56a ^
d6c8efa5d ^


ce859d8c1 ^
f760bc243 ^


b870744d5 ^
7e747d11c ^
765f27462 ^
b044523c5 ^



765f27462 ^





b870744d5 ^
f760bc243 ^
4b7655fd1 ^

ce859d8c1 ^
f760bc243 ^

8e0941576 ^

fd62116f6 ^
0ff1190fe ^
ebb15505d ^
6cb8bf804 ^
a432aedb5 ^

6cb8bf804 ^
2895ad70c ^
7e747d11c ^
28a116a47 ^

2895ad70c ^

0ff1190fe ^
05c52ff34 ^
793931937 ^
05c52ff34 ^


5536f7499 ^

05c52ff34 ^





5536f7499 ^

e99721a59 ^
2ac3e1237 ^
7b36d3d6f ^
66a255652 ^

e99721a59 ^
5536f7499 ^
e99721a59 ^














12bac28d2 ^
ce859d8c1 ^
c314f8e65 ^
12bac28d2 ^
226595515 ^
5536f7499 ^
73a8b950c ^
8241e5502 ^
8948a9715 ^


c3e5c6c32 ^








6378fbd66 ^
ed1d41c51 ^
6278b5d89 ^





6378fbd66 ^


b870744d5 ^
cd0256136 ^
bf592c4e9 ^
cd0256136 ^
226595515 ^
cd0256136 ^


6378fbd66 ^


7e747d11c ^
cd0256136 ^






c3e5c6c32 ^
cd0256136 ^



672dc5cd8 ^

bf592c4e9 ^


cd0256136 ^



fc0bcccc1 ^



ce859d8c1 ^
cd0256136 ^
20b011de1 ^
cd0256136 ^
4887f6410 ^
cd0256136 ^

5bcf8bcb5 ^
cd0256136 ^
6378fbd66 ^
9134bb9cf ^





9134bb9cf ^
f86fc5316 ^



979148e86 ^


dd24004fa ^
f86fc5316 ^




















9134bb9cf ^
862c0ef83 ^
9134bb9cf ^
ce859d8c1 ^
9134bb9cf ^





862c0ef83 ^
7e747d11c ^
479373ad7 ^

862c0ef83 ^
40ec7be45 ^
862c0ef83 ^
7e747d11c ^


16bc546ae ^
2a278f6eb ^


72b89eff8 ^
2a278f6eb ^
862c0ef83 ^

52b241fd5 ^

40ec7be45 ^
52b241fd5 ^
ce859d8c1 ^
52b241fd5 ^

fb27357b6 ^





2bc07554c ^
84d122b5c ^





2bc07554c ^
963292f72 ^












63d29ddd6 ^


























20b011de1 ^
63d29ddd6 ^






































22dc76a36 ^
e25474154 ^
3aa7c2232 ^
3c773c189 ^
328e7a100 ^
e25474154 ^
d9ee37751 ^
da6046dcb ^
d447c0fe3 ^
ce859d8c1 ^
2bc07554c ^
644d645ea ^
b870744d5 ^
7e747d11c ^
b595fc834 ^
dba25d262 ^
47335aab4 ^
12bac28d2 ^
8ef66b973 ^










b1d4dfa6b ^
a2480efd6 ^
7e747d11c ^
ea409fb15 ^
a2480efd6 ^
226595515 ^
a2480efd6 ^

ce859d8c1 ^
030eac86c ^
2ac3e1237 ^
030eac86c ^
7e747d11c ^
b0a98cc01 ^
ce859d8c1 ^
9c8bc3a24 ^
7e747d11c ^

1d02f2ea5 ^
7e747d11c ^
9c8bc3a24 ^
1d02f2ea5 ^
7e747d11c ^
9c8bc3a24 ^

1d02f2ea5 ^

ccd2934e4 ^
1d02f2ea5 ^

7e747d11c ^
e63b673ce ^




672dc5cd8 ^
e63b673ce ^
672dc5cd8 ^


e63b673ce ^


























226595515 ^
1d02f2ea5 ^

ce859d8c1 ^
1d02f2ea5 ^
7e747d11c ^
1d02f2ea5 ^
ccd2934e4 ^
ce859d8c1 ^
d96f25619 ^



7720c0aaf ^
ce859d8c1 ^
2bc07554c ^
c94647aec ^
2bc07554c ^
61ea85687 ^
5f515410a ^
bc2eb0ea9 ^
121b9e26f ^



12bac28d2 ^

796aafe7e ^
12bac28d2 ^

ce859d8c1 ^
7e747d11c ^
52b241fd5 ^

e25474154 ^


f9eec2aa1 ^
e25474154 ^


ceadf54d7 ^
a28090a8f ^
ed2fb36bc ^


8805829d7 ^
9ffab44c3 ^
a49b06a52 ^

9ffab44c3 ^
a49b06a52 ^
cda92048b ^
7e747d11c ^
af7c92c00 ^
8241e5502 ^
73a8b950c ^
8241e5502 ^
7e747d11c ^

5f4e98bbc ^

226595515 ^
5f4e98bbc ^

7e747d11c ^
e4081a720 ^
ce859d8c1 ^
7e747d11c ^
226595515 ^
5f4e98bbc ^


e25474154 ^
ac271e76b ^
0e1304a3e ^
2d726711f ^
ce859d8c1 ^
2d726711f ^




ce859d8c1 ^
752052e90 ^


21ce7b2af ^



0e1304a3e ^
fce2ff161 ^
0e1304a3e ^


ac271e76b ^
dc669155e ^
b870744d5 ^
ce859d8c1 ^
79aaf213d ^
5b5b02cb1 ^
ce859d8c1 ^
c1f1f8419 ^
b595fc834 ^
e25474154 ^
b870744d5 ^
0e1304a3e ^


5536f7499 ^

0e1304a3e ^




226595515 ^
0e1304a3e ^
b595fc834 ^
97e26967f ^
ae27f8ac5 ^
d5c9255ca ^





0e1304a3e ^


5536f7499 ^
e25474154 ^
b870744d5 ^
e25474154 ^

81ddc6778 ^
6e8ed8c6f ^
b595fc834 ^
7c62de0cb ^
03fa9a904 ^



e09c259bf ^
79aaf213d ^
39ad9a69a ^
6c2050912 ^
9c126282b ^
6378fbd66 ^
064f29621 ^

0d23419e6 ^

6c2050912 ^
fb27357b6 ^
1bb117cd7 ^


760242b87 ^

1bb117cd7 ^









226595515 ^
8d99753d6 ^
8c4b7129b ^

1bb117cd7 ^

dc669155e ^



7d6500f1d ^
08261cb9e ^



752052e90 ^
3aa7c2232 ^
752052e90 ^
a4e2b0c15 ^
837b77b0a ^
e25474154 ^
4741e8f9a ^
837b77b0a ^






21fc8b4d4 ^

b870744d5 ^
bf592c4e9 ^



b870744d5 ^
4741e8f9a ^
bf592c4e9 ^















b870744d5 ^
350396e1c ^
ce859d8c1 ^
ed2fb36bc ^
28b4db447 ^
b870744d5 ^
7e9442084 ^
ce859d8c1 ^
bf592c4e9 ^
a4e2b0c15 ^
cabbcd411 ^
0aa908c86 ^
bf592c4e9 ^
0aa908c86 ^
b92fd3028 ^
cabbcd411 ^
b92fd3028 ^
fb27357b6 ^
bf592c4e9 ^
d912d1837 ^
fb27357b6 ^
59d19946c ^
fb27357b6 ^
b0b716fca ^
bf592c4e9 ^
d912d1837 ^
7d82df20b ^
d912d1837 ^
7d82df20b ^
d912d1837 ^
7d82df20b ^
d912d1837 ^
bf592c4e9 ^
d912d1837 ^

bf592c4e9 ^
0ead36dae ^
bf592c4e9 ^
b870744d5 ^
7d82df20b ^
ed2fb36bc ^
5b5985240 ^
2ac3ba713 ^



abe0725ab ^
d912d1837 ^
bf592c4e9 ^
d912d1837 ^
ceadf54d7 ^


612cd25d5 ^

bf592c4e9 ^
0d744cccc ^
e316665b7 ^

a4e2b0c15 ^
7d82df20b ^
4be36d77f ^


ce859d8c1 ^

b870744d5 ^
f760bc243 ^
1d02f2ea5 ^
4741e8f9a ^
b870744d5 ^
d938c6411 ^
50b6f6996 ^
226595515 ^
d938c6411 ^


4741e8f9a ^
b870744d5 ^
ce859d8c1 ^
674c05f42 ^
7e747d11c ^
50b6f6996 ^

d938c6411 ^














b870744d5 ^
d938c6411 ^




f760bc243 ^
d938c6411 ^




b870744d5 ^
4741e8f9a ^
d938c6411 ^





8241e5502 ^









d938c6411 ^















1d02f2ea5 ^
d938c6411 ^

50b6f6996 ^
d938c6411 ^


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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299

 
                            
                                         



                                                   
 
                                                             
                       
 
     
                                                                



                                                         
                                                                     
                                                             
                                                                                                  




                                                                                         













                                                                                   
                                                                      
                                               
                              
       

                                                   
                                                               
 
                                                      
                                          
                                          
                                                  

                                                         

                                                

                                               
     


                          
                             



                                         
                                 

                                       
                           
                                                                            
                                                  
                         
                                                               
                        
                       
                     


                                     
                      

                                               
                                        
                                                  
           
                                            
                                     
                                      
                             

                                                           

                                                               
                                                                               
                                                                    
                                                                                                          
                                                                            
                                                                                       
               
                                                                       
             
                                                                 
                             


                   
                                                             
                                                                                                
                                   
                                                                                

                                                           
                       
                                                                            
                     
                                   
                 
             
                  
                            
                                       
                                
                    

                                             
                                   
         
                                  

                             
                               

                                                               
                                
                                      
                                                            
                                       
 
                        
                       
                    
                              
                    
                                                           

                                  
                                                                                
                                                           




                                                                                                
                                               
                                            

                                             
                                                                                      
 
                                                        
                                        

                                         
                                           
                                                                          
                                                             
                                                            
                                                                                             
                                                      
                                                  
       
                                                                 
                                                   
 
                                                                             

                                        
                           
                                                                               
                                           
       
                                                                   
                                                   
 




                                                                          

                                                            

                                        
                                           

                                                                      
       
                                                                     
                                                   
 
                                                                                  
                

                                          


                                                    
                          
                                                                   
                    
                                           
       
                                    
 










                                                                 


                                                                                   
                         
                        
                           
                                                       




                                                                         












                                                                                           
                                                             

                                                    
                                                                              
                               
 
                                                             
                   
                              

                                          

                                                                    
                                                 
 
                                                      
                                                  
 


                                                        
 
                             
                
                                                                      
 




                                                                                    

                                                             
                                                                                       
                                 
                                                                                                        
                                 
                                                                                   
 
                
                                      
                                              
                                    
         
                                            
 

                                                                                                  
 
                                                    
                                                  
 
                       
 
                                                          
              
                

                                         











                                                                       
         
                                                                             
                                                                                                               
           
                                                         
                                              
       
                                                                   
                                            
 
                                                  

                                   

                                                                            




                                                    
                      
                                    
                                                                 



                                                             
                                                                                   
                                                           
                                                                                            
                                            
                                                               
                                                                               
                                                                                        
                                                                                                 










                                                               
                                                        


                                                           
                                                              
 
                                                          
                 
                
                                 

                                     
                                                                             
                                                               


                                                               
                                                                                                 
                               
                                                               
                                                            
                                    

                                                                             


                                                                         
                                           
       
                                                              
                                            
 








                                                                      
                                                            
                                            

                                        

                                   
                                                                                                
                                           
       
                                                                     
                                            
 
                                                              
                
                                                 
                                          
              

                                                      
 
                                                          
                

                                          
                          
                 

                                                

                                                         

                                               
                                
                               
                                      



                                                                                                            
         
                                                   
                        

                                           


                              
                                              
                                                                                      
           


                                    
                                                     
                         
                                              
                                        
                              
                                      


                                                                                   

                                                       
                              
                         
                  

                                                                      

                                          
                                                      

                                      
                                       
                                                                           
             
                                                                                   
         
                               
       
                                
 


                                                              
                                
                                                
             
              
                                                                      
             



                                                     
                 
       
                                             
 

                                                                          

                              
                                                   
                                       
                                                            
 
                                                                              
                                 

                             

                                                                                    

                                                             
 


                                    
                                                                       
                                                                 
 
                                                 
                                                     
                              
                                                    
 
                                                   
                                                       
                
                                
                                                      
                         
                                
                                                      
       
                                            

                
                                                                   
                                         
                                
                        
                     
                         
                   
                    
                                                      
         
                                             


                                                              
                                                                           


                                                        
                                                           
                                       
                                                 
                                                                 


                                                                         
                                 

                    
                             
                                      
 

                                                                 
 
                                                       
                           

                                                                       
                                      


                                                          
                                           
                                         
 
                                             

                                                      
                               
       









                                                                      
                                     
 
                                                                             
                                                                           
 














                                                                           
                                            












                                                   

                      

                                                                     

                                 









                                                          
                                 
                     


                                                                              
 
                                                                          
                                                   
                     
                                 

                                                                         
                                                                       
          
                                      
                           
                         
                                                         
                                                                                      
               
                       
                      
                                  
           
             
                                                                 
                        
               
                                                           
                              
                                                                                 
                                  
                                                            

                                                                    
                                                                                     
 



                          
                  
                                     
                                        
              
                                  
                                                         
                                                      
                         
                                   


                                                                                    


                                                                             

                                                               
              
 
                                                                             
                                                                           
                     
             
               
                       
                                                  

                       
                                             
                 
                  
                      
                                     
                                   
                                            
                                                                      
                                                              
             
                                                         
                
                                     
                                        
               
                                     
                                
                                                      
                                    

                                              
                                                                                    
                                                      
                              
                  
                       
                                                                             

                                             
               
                                                    
              
                                                                       
               
                                                  
                                                                 
                       
                                                                      
                                
                 
                                   
                
                                                                               
                                       
                  

                                               
                                

                                    
                                                   

                        
                                      



                                                                                                            
                                    

                                                              



                                                                
                      
                            
                                                    

                      
                                  

                                                            
                                             
                                
                                                              
              
                                          
                                                                             


                              
                                                   

                                           
                         
                      
                                      
           

                                                                     

                                            
                                                                                 
                


                                           
                                 

                                                                        
                                      
             
               



                                                                                     
                            
                                                           
                                                                                        
                                                
                                                 
                                                
           
                              
            
                                                                  
 
                                                        
            
                                        
                      
                                                                                              

                            











                                                                                               
 
                                                                                  
                
                                     
                          
             
                                 
                                                                 
                              

                                           
                                             
                  
                                                                                           
         
                                                    





                                                                            
                                         


                                                                                 


                                                                       

                                        
                                                                                             

                                                                                 
                  
                      
                                                                           
                                           
                             
                                                
                                   
                                                    



                                               


                                                                             

                                                         
                                
                                                                                
                  
                                   

                                                      

                                                                
 







                                                                            
                                                                                      
                                                     




                                                           
                                                                                     

                                         
                            
                                               
                              
                   
                                                         



                                            

                                            
                                                                            
                                 
                                                                                        
                                                   
             
                                                                                             

                                                   

                                                                               
                                                  








                                                  

                                                
                 
                                                                                   
                                 
 



                                                     
                         





                                       
                                                                 
                     

                                                      
                                     
                             
                   

                                                              
         
                                                         
                                                        
                                     
                    
                   
       




                                                     
 
                            
                                               

                                       









                                                                     


                                                     


                                                    
                                                          




                                                     
                                 


                    


                                                                         

                                                       
                                                           
                                                                             



                                                                           
                     
                
                                                                                                    
 
              
                                                            
                                       
            
                                                               


                                                              
                                               
                                                                               

                                                                        
                                                                    
 
                
                                          
                                              
                                                                

                                                               

                                                            

                                                              
                                                                                   
 
                
                          
                                                        
                                            
 






                                   
                              
                                    
 
                                             
                                         
                                                  
                                                  



                                                      
                                                                         

                                                                 
                                                                                     
         

                                     
                                                                                           
                                          
                         
                               
                            
 
                   
                                             
                               
 


                                                  

                                                

                                                
 


                                                 
                                                                                      
 
                                                               
                                                        

                                                                      
                                               
                                                                                      
 
                   
                                                 
                                                                       

                                                           
                                   
                                                                                  
 

                                        
                       
                             
                          
                                     
 
                                                   

                       

                                 
 
                         
                               
                                          

                                             
 
                             
                                         




                                                                 

                                                                
                                      
 
                                                                 
                                   
                                  
                                                                             
                                       
 
                    
                                                  
                                            

                                     
                                                  
 
               
 
                                                                        
                                      
                           
                                      





                                                                           


                                   




                                                                          
                                                                        

                                                                           
                                           
 
                               
                                 


                                    
                                                          


                                                                        
                                 
                                       
                          
                 
 

                     





                                                                          
                               
 
                                   

                      

                             

                                        
 
               
                                              
                                                                    
                                                                                     
                                        
                                                                                                                             
 
 
                  
                 









                                                                              
                          
                        
                                                          
                      




                                                       
                                                                

                                                       
 
                    
                     
                                 
                                                                                            
 
                                  





                                                                         
                                                                      
                                               
 

                                                       

                                                     
                                        
                                                             
                                              


                                              
 
                                      
                                                             
                                       
                                    
                                                                         
              
 
                         
                                                                                     
                                         


                                                                                    

                                             



                                                                             
                                                                                     
                            


                                            
                                                             
                                                       
                                                                            
                         
                            
                                 
                  

                                            
                                  
                                  
                                    
                                      
                           
                            
 
              

                                 
 
                                                                      
                                                                

                                                                   
                        



                                                                               
                                                

           
              
                                                                         
                                     
                                                                     
                                                                             
                                                                           
                                                       
                                                         
                                
               


                                              
           

                                                                         
                                               




                                                   
                                  

                                 
                                                                
                                                              
                                      
                                                         
                   
                                 
                                 



                                                               
                      
 
                                                    
                           


                                              
                                       
                                            

                                       
                                                                 
                                 



                                        
                  
                      
       
                

                                                              
                             

                                      
                              
              



                                         
                
               
                                  
                             
 
                                                                
                                 
                                 
 
                                                                                     



                        
                   
                                             
               
                
                                                                       
       
                                                  
                                                                          

                                                                   

                                      
                                                                           


                                               
                                                                     
                  
                                                                   
                                             
                                          
 

                                                                     

              
                                                      
                                      
 

                             
                                    
                            
                                                
 
                         

                                                     
                       

                               

                                                                
                                                                          
                                          
       
                              
                     
                                 
 
                          


                                                                                               
                                                     


                                            
 
                            
                             



                                                                       





                                                       
 
                  

                                                                
                                                                      

                                                

                                                             
 
                                         
                                      
                                             

                                                                                            
                       
                                          
                    

                                                               

                                               
 
                                                                
                                                                                               


                                 

                                  





                                                            

                                  
 
                                                             
                                                  

                              
                                               
                                                                      














                                                            
       
                                                                           
                         
 
                                                                 
                                              
                                                         
                                                       


                








                                                      
                                                              
                                                       





                                              


                    
 
                                                  
                                          
                             
                                                                                  


                                    


                                      
                     






                      
                                               



                                                                     

                                       


                                                    



                                   



                                                              
                                                      
                                                                          
                                                                        
                              
                                 

                          
                                       
               
 





                                                                           
         



                                                  


                                               
                                                      




















                                                                                                 
 
                                                
                                                               
                              





                                                       
              
                                                                 

                                                                     
                              
                                                                            
                


                                                                      
                             


                                                                                        
                                                   
                                                                                   

               

                                                           
                                          
       
                                                 

                           





                                                                            
                                                           





                                             
                                                            












                                                                                              


























                                                                               
                                                






































                                                                                  
                                                             
              
                     
 
                                                   
             
                            
                  
                                                         
                                
                                     
                                                              
           
                                                      
         
                                       
                                                     
                 










                                                                    
                                      
                                              
                                         
                                                                                                       
                                    
                                             

                                     
                                                                  
                                      
                                      
         
                                           
                                                                
                                    
           

                                        
                     
                                                          
                                                  
                       
                                                          

                                                  

                                                                
                              

                  
                                             




                                                                                           
                                                                       
                                        


                                                                                               


























                                                                                        
                                               

                                       
                                                                      
             
                                                  
                                          
             
                                                                    



                                             
                               
                                    
                                         
                                                                                 
                                       
                                                                                        
                                           
           



                                                       

                                         
                                                                                   

                                             
                                   
                   

                                                                 


                                                                              
                                                                                             


                                                
                                                          
            


                                                          
                                                
                                 

                                                         
               
                                           
             
                                        
                       
                        
                                                                      
                                                           

                                                     

                                                 
                                                                  

                                            
                                          
                                      
                                                                     
                      
                                             


                                                  
                                            
               
                                
                          
                                                                               




                                                        
                                                   


                                



                                                              
                                               
                                                           


                                                   
                          
                              
                    
                                                                         
                                              
                                                        
                                                                        
                         
                     
                    
         


                                                

                                      




                                                                                 
                                    
                     
           
                               
                                                                       





                                                                            


                                            
                                                     
                  
           

                           
                                
                          
         
                           



                                                                                                  
                                          
                                              
                                                       
                                             
                                                       
                                                     

                                                   

                                                                
                                                   
                                                      


                                                                          

                                              









                                                                                     
                                             
         

                                                                      

                                                          



                                                         
       



                                                                              
                
                     
 
                                                                         
                                                                       
                   
                   






                                                                                

                                                                                  
 



                                                                             
                                             
              















                                                                              
             
                                                         
                                                         
                                                   
                                 
              
                                                                     
                                                         
                                                                                
                                                                    
           
                          
                                                  
         
                                                 
           
                                             
                      
                                                
                                         
             
                                          
                                         
               
                                            
            
                                                        
                
                                                            
              
                                                          
            
                                                            

                                         
                                                          
                     
                                                                     
          
                                                           
                                                   
                                 



                                                                    
                                        
              
                                                              
                                         


                                                

                                           
                                            
                        

                  
                                                                         
                                                                  


                                                                 

                                                       
 
                                                          
                                                   
 
                                                                             
 
                                                    
                                                              


                                            
                      
                               
                             
          
                     

                














                                                                          
 




                                                          
             




                                                                               
 
                    





                                                   









                                                                  















                                                                                         
 

                                                                           
                               


                               
#
#
#           The Nim 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

const
  errStringOrIdentNodeExpected = "string or ident node expected"
  errStringLiteralExpected = "string literal expected"
  errIntLiteralExpected = "integer literal expected"
  errWrongNumberOfVariables = "wrong number of variables"
  errInvalidOrderInEnumX = "invalid order in enum '$1'"
  errOverflowInEnumX = "The enum '$1' exceeds its maximum value ($2)"
  errOrdinalTypeExpected = "ordinal type expected; given: $1"
  errSetTooBig = "set is too large; use `std/sets` for ordinal types with more than 2^16 elements"
  errBaseTypeMustBeOrdinal = "base type of a set must be an ordinal"
  errInheritanceOnlyWithNonFinalObjects = "inheritance only works with non-final objects"
  errXExpectsOneTypeParam = "'$1' expects one type parameter"
  errArrayExpectsTwoTypeParams = "array expects two type parameters"
  errInvalidVisibilityX = "invalid visibility: '$1'"
  errXCannotBeAssignedTo = "'$1' cannot be assigned to"
  errIteratorNotAllowed = "iterators can only be defined at the module's top level"
  errXNeedsReturnType = "$1 needs a return type"
  errNoReturnTypeDeclared = "no return type declared"
  errTIsNotAConcreteType = "'$1' is not a concrete type"
  errTypeExpected = "type expected"
  errXOnlyAtModuleScope = "'$1' is only allowed at top level"
  errDuplicateCaseLabel = "duplicate case label"
  errMacroBodyDependsOnGenericTypes = "the macro body cannot be compiled, " &
    "because the parameter '$1' has a generic type"
  errIllegalRecursionInTypeX = "illegal recursion in type '$1'"
  errNoGenericParamsAllowedForX = "no generic parameters allowed for $1"
  errInOutFlagNotExtern = "the '$1' modifier can be used only with imported types"

proc newOrPrevType(kind: TTypeKind, prev: PType, c: PContext): PType =
  if prev == nil or prev.kind == tyGenericBody:
    result = newTypeS(kind, c)
  else:
    result = prev
    if result.kind == tyForward: result.kind = kind
  #if kind == tyError: result.flags.incl tfCheckedForDestructor

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

proc semEnum(c: PContext, n: PNode, prev: PType): PType =
  if n.len == 0: return newConstraint(c, tyEnum)
  elif n.len == 1:
    # don't create an empty tyEnum; fixes #3052
    return errorType(c)
  var
    counter, x: BiggestInt
    e: PSym
    base: PType
    identToReplace: ptr PNode
  counter = 0
  base = nil
  result = newOrPrevType(tyEnum, prev, c)
  result.n = newNodeI(nkEnumTy, n.info)
  checkMinSonsLen(n, 1, c.config)
  if n[0].kind != nkEmpty:
    base = semTypeNode(c, n[0][0], nil)
    if base.kind != tyEnum:
      localError(c.config, n[0].info, "inheritance only works with an enum")
    counter = toInt64(lastOrd(c.config, base)) + 1
  rawAddSon(result, base)
  let isPure = result.sym != nil and sfPure in result.sym.flags
  var symbols: TStrTable
  initStrTable(symbols)
  var hasNull = false
  for i in 1..<n.len:
    if n[i].kind == nkEmpty: continue
    case n[i].kind
    of nkEnumFieldDef:
      if n[i][0].kind == nkPragmaExpr:
        e = newSymS(skEnumField, n[i][0][0], c)
        identToReplace = addr n[i][0][0]
        pragma(c, e, n[i][0][1], enumFieldPragmas)
      else:
        e = newSymS(skEnumField, n[i][0], c)
        identToReplace = addr n[i][0]
      var v = semConstExpr(c, n[i][1])
      var strVal: PNode = nil
      case skipTypes(v.typ, abstractInst-{tyTypeDesc}).kind
      of tyTuple:
        if v.len == 2:
          strVal = v[1] # second tuple part is the string value
          if skipTypes(strVal.typ, abstractInst).kind in {tyString, tyCstring}:
            if not isOrdinalType(v[0].typ, allowEnumWithHoles=true):
              localError(c.config, v[0].info, errOrdinalTypeExpected % typeToString(v[0].typ, preferDesc))
            x = toInt64(getOrdValue(v[0])) # first tuple part is the ordinal
            n[i][1][0] = newIntTypeNode(x, getSysType(c.graph, unknownLineInfo, tyInt))
          else:
            localError(c.config, strVal.info, errStringLiteralExpected)
        else:
          localError(c.config, v.info, errWrongNumberOfVariables)
      of tyString, tyCstring:
        strVal = v
        x = counter
      else:
        if not isOrdinalType(v.typ, allowEnumWithHoles=true):
          localError(c.config, v.info, errOrdinalTypeExpected % typeToString(v.typ, preferDesc))
        x = toInt64(getOrdValue(v))
        n[i][1] = newIntTypeNode(x, getSysType(c.graph, unknownLineInfo, tyInt))
      if i != 1:
        if x != counter: incl(result.flags, tfEnumHasHoles)
        if x < counter:
          localError(c.config, n[i].info, errInvalidOrderInEnumX % e.name.s)
          x = counter
      e.ast = strVal # might be nil
      counter = x
    of nkSym:
      e = n[i].sym
    of nkIdent, nkAccQuoted:
      e = newSymS(skEnumField, n[i], c)
      identToReplace = addr n[i]
    of nkPragmaExpr:
      e = newSymS(skEnumField, n[i][0], c)
      pragma(c, e, n[i][1], enumFieldPragmas)
      identToReplace = addr n[i][0]
    else:
      illFormedAst(n[i], c.config)
    e.typ = result
    e.position = int(counter)
    let symNode = newSymNode(e)
    if identToReplace != nil and c.config.cmd notin cmdDocLike:
      # A hack to produce documentation for enum fields.
      identToReplace[] = symNode
    if e.position == 0: hasNull = true
    if result.sym != nil and sfExported in result.sym.flags:
      e.flags.incl {sfUsed, sfExported}

    result.n.add symNode
    styleCheckDef(c, e)
    onDef(e.info, e)
    if sfGenSym notin e.flags:
      if not isPure:
        addInterfaceOverloadableSymAt(c, c.currentScope, e)
      else:
        declarePureEnumField(c, e)
    if (let conflict = strTableInclReportConflict(symbols, e); conflict != nil):
      wrongRedefinition(c, e.info, e.name.s, conflict.info)
    if counter == high(typeof(counter)):
      if i > 1 and result.n[i-2].sym.position == high(int):
        localError(c.config, n[i].info, errOverflowInEnumX % [e.name.s, $high(typeof(counter))])
    else:
      inc(counter)
  if isPure and sfExported in result.sym.flags:
    addPureEnum(c, LazySym(sym: result.sym))
  if tfNotNil in e.typ.flags and not hasNull:
    result.flags.incl tfRequiresInit
  setToStringProc(c.graph, result, genEnumToStrProc(result, n.info, c.graph, c.idgen))

proc semSet(c: PContext, n: PNode, prev: PType): PType =
  result = newOrPrevType(tySet, prev, c)
  if n.len == 2 and n[1].kind != nkEmpty:
    var base = semTypeNode(c, n[1], nil)
    addSonSkipIntLit(result, base, c.idgen)
    if base.kind in {tyGenericInst, tyAlias, tySink}: base = lastSon(base)
    if base.kind notin {tyGenericParam, tyGenericInvocation}:
      if not isOrdinalType(base, allowEnumWithHoles = true):
        localError(c.config, n.info, errOrdinalTypeExpected % typeToString(base, preferDesc))
      elif lengthOrd(c.config, base) > MaxSetElements:
        localError(c.config, n.info, errSetTooBig)
  else:
    localError(c.config, n.info, errXExpectsOneTypeParam % "set")
    addSonSkipIntLit(result, errorType(c), c.idgen)

proc semContainerArg(c: PContext; n: PNode, kindStr: string; result: PType) =
  if n.len == 2:
    var base = semTypeNode(c, n[1], nil)
    if base.kind == tyVoid:
      localError(c.config, n.info, errTIsNotAConcreteType % typeToString(base))
    addSonSkipIntLit(result, base, c.idgen)
  else:
    localError(c.config, n.info, errXExpectsOneTypeParam % kindStr)
    addSonSkipIntLit(result, errorType(c), c.idgen)

proc semContainer(c: PContext, n: PNode, kind: TTypeKind, kindStr: string,
                  prev: PType): PType =
  result = newOrPrevType(kind, prev, c)
  semContainerArg(c, n, kindStr, result)

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

proc semVarOutType(c: PContext, n: PNode, prev: PType; flags: TTypeFlags): PType =
  if n.len == 1:
    result = newOrPrevType(tyVar, prev, c)
    result.flags = flags
    var base = semTypeNode(c, n[0], nil)
    if base.kind == tyTypeDesc and not isSelf(base):
      base = base[0]
    if base.kind == tyVar:
      localError(c.config, n.info, "type 'var var' is not allowed")
      base = base[0]
    addSonSkipIntLit(result, base, c.idgen)
  else:
    result = newConstraint(c, tyVar)

proc isRecursiveType(t: PType, cycleDetector: var IntSet): bool =
  if t == nil:
    return false
  if cycleDetector.containsOrIncl(t.id):
    return true
  case t.kind
  of tyAlias, tyGenericInst, tyDistinct:
    return isRecursiveType(t.lastSon, cycleDetector)
  else:
    return false

proc fitDefaultNode(c: PContext, n: PNode): PType =
  let expectedType = if n[^2].kind != nkEmpty: semTypeNode(c, n[^2], nil) else: nil
  n[^1] = semConstExpr(c, n[^1], expectedType = expectedType)
  let oldType = n[^1].typ
  n[^1].flags.incl nfSem
  if n[^2].kind != nkEmpty:
    if expectedType != nil and oldType != expectedType:
      n[^1] = fitNodeConsiderViewType(c, expectedType, n[^1], n[^1].info)
    result = n[^1].typ
  else:
    result = n[^1].typ

proc isRecursiveType*(t: PType): bool =
  # handle simple recusive types before typeFinalPass
  var cycleDetector = initIntSet()
  isRecursiveType(t, cycleDetector)

proc addSonSkipIntLitChecked(c: PContext; father, son: PType; it: PNode, id: IdGenerator) =
  let s = son.skipIntLit(id)
  father.sons.add(s)
  if isRecursiveType(s):
    localError(c.config, it.info, "illegal recursion in type '" & typeToString(s) & "'")
  else:
    propagateToOwner(father, s)

proc semDistinct(c: PContext, n: PNode, prev: PType): PType =
  if n.len == 0: return newConstraint(c, tyDistinct)
  result = newOrPrevType(tyDistinct, prev, c)
  addSonSkipIntLitChecked(c, result, semTypeNode(c, n[0], nil), n[0], c.idgen)
  if n.len > 1: result.n = n[1]

proc semRangeAux(c: PContext, n: PNode, prev: PType): PType =
  assert isRange(n)
  checkSonsLen(n, 3, c.config)
  result = newOrPrevType(tyRange, prev, c)
  result.n = newNodeI(nkRange, n.info)
  # always create a 'valid' range type, but overwrite it later
  # because 'semExprWithType' can raise an exception. See bug #6895.
  addSonSkipIntLit(result, errorType(c), c.idgen)

  if (n[1].kind == nkEmpty) or (n[2].kind == nkEmpty):
    localError(c.config, n.info, "range is empty")

  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(c.idgen)

  let hasUnknownTypes = c.inGenericContext > 0 and
    rangeT[0].kind == tyFromExpr or rangeT[1].kind == tyFromExpr

  if not hasUnknownTypes:
    if not sameType(rangeT[0].skipTypes({tyRange}), rangeT[1].skipTypes({tyRange})):
      typeMismatch(c.config, n.info, rangeT[0], rangeT[1], n)

    elif not isOrdinalType(rangeT[0]) and rangeT[0].kind notin {tyFloat..tyFloat128} or
        rangeT[0].kind == tyBool:
      localError(c.config, n.info, "ordinal or float type expected, but got " & typeToString(rangeT[0]))
    elif enumHasHoles(rangeT[0]):
      localError(c.config, n.info, "enum '$1' has holes" % typeToString(rangeT[0]))

  for i in 0..1:
    if hasUnresolvedArgs(c, range[i]):
      result.n.add makeStaticExpr(c, range[i])
      result.flags.incl tfUnresolved
    else:
      result.n.add semConstExpr(c, range[i])

    if result.n[i].kind in {nkFloatLit..nkFloat64Lit} and result.n[i].floatVal.isNaN:
      localError(c.config, n.info, "NaN is not a valid range " & (if i == 0: "start" else: "end"))

  if weakLeValue(result.n[0], result.n[1]) == impNo:
    localError(c.config, n.info, "range is empty")

  result[0] = rangeT[0]

proc semRange(c: PContext, n: PNode, prev: PType): PType =
  result = nil
  if n.len == 2:
    if isRange(n[1]):
      result = semRangeAux(c, n[1], prev)
      if not isDefined(c.config, "nimPreviewRangeDefault"):
        let n = result.n
        if n[0].kind in {nkCharLit..nkUInt64Lit} and n[0].intVal > 0:
          incl(result.flags, tfRequiresInit)
        elif n[1].kind in {nkCharLit..nkUInt64Lit} and n[1].intVal < 0:
          incl(result.flags, tfRequiresInit)
        elif n[0].kind in {nkFloatLit..nkFloat64Lit} and
            n[0].floatVal > 0.0:
          incl(result.flags, tfRequiresInit)
        elif n[1].kind in {nkFloatLit..nkFloat64Lit} and
            n[1].floatVal < 0.0:
          incl(result.flags, tfRequiresInit)
    else:
      if n[1].kind == nkInfix and considerQuotedIdent(c, n[1][0]).s == "..<":
        localError(c.config, n[0].info, "range types need to be constructed with '..', '..<' is not supported")
      else:
        localError(c.config, n[0].info, "expected range")
      result = newOrPrevType(tyError, prev, c)
  else:
    localError(c.config, n.info, errXExpectsOneTypeParam % "range")
    result = newOrPrevType(tyError, prev, c)

proc semArrayIndex(c: PContext, n: PNode): PType =
  if isRange(n):
    result = semRangeAux(c, n, nil)
  elif n.kind == nkInfix and n[0].kind == nkIdent and n[0].ident.s == "..<":
    result = errorType(c)
  else:
    let e = semExprWithType(c, n, {efDetermineType})
    if e.typ.kind == tyFromExpr:
      result = makeRangeWithStaticExpr(c, e.typ.n)
    elif e.kind in {nkIntLit..nkUInt64Lit}:
      if e.intVal < 0:
        localError(c.config, n.info,
          "Array length can't be negative, but was " & $e.intVal)
      result = 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 semArrayIndex(c, e.sym.ast)
      if e.typ.lastSon.kind != tyGenericParam and not isOrdinalType(e.typ.lastSon):
        let info = if n.safeLen > 1: n[1].info else: n.info
        localError(c.config, info, errOrdinalTypeExpected % typeToString(e.typ, preferDesc))
      result = makeRangeWithStaticExpr(c, e)
      if c.inGenericContext > 0: result.flags.incl tfUnresolved
    elif e.kind in (nkCallKinds + {nkBracketExpr}) and hasUnresolvedArgs(c, e):
      if not isOrdinalType(e.typ.skipTypes({tyStatic, tyAlias, tyGenericInst, tySink})):
        localError(c.config, n[1].info, errOrdinalTypeExpected % typeToString(e.typ, preferDesc))
      # 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).
      result = makeRangeWithStaticExpr(c, e)
    elif e.kind == nkIdent:
      result = e.typ.skipTypes({tyTypeDesc})
    else:
      let x = semConstExpr(c, e)
      if x.kind in {nkIntLit..nkUInt64Lit}:
        result = makeRangeType(c, 0, x.intVal-1, n.info,
                             x.typ.skipTypes({tyTypeDesc}))
      else:
        result = x.typ.skipTypes({tyTypeDesc})
        #localError(c.config, n[1].info, errConstExprExpected)

proc semArray(c: PContext, n: PNode, prev: PType): PType =
  var base: PType
  if n.len == 3:
    # 3 = length(array indx base)
    let indx = semArrayIndex(c, n[1])
    var indxB = indx
    if indxB.kind in {tyGenericInst, tyAlias, tySink}: indxB = lastSon(indxB)
    if indxB.kind notin {tyGenericParam, tyStatic, tyFromExpr}:
      if indxB.skipTypes({tyRange}).kind in {tyUInt, tyUInt64}:
        discard
      elif not isOrdinalType(indxB):
        localError(c.config, n[1].info, errOrdinalTypeExpected % typeToString(indxB, preferDesc))
      elif enumHasHoles(indxB):
        localError(c.config, n[1].info, "enum '$1' has holes" %
                   typeToString(indxB.skipTypes({tyRange})))
    base = semTypeNode(c, n[2], nil)
    # ensure we only construct a tyArray when there was no error (bug #3048):
    result = newOrPrevType(tyArray, prev, c)
    # bug #6682: Do not propagate initialization requirements etc for the
    # index type:
    rawAddSonNoPropagationOfTypeFlags(result, indx)
    addSonSkipIntLit(result, base, c.idgen)
  else:
    localError(c.config, n.info, errArrayExpectsTwoTypeParams)
    result = newOrPrevType(tyError, prev, c)

proc semIterableType(c: PContext, n: PNode, prev: PType): PType =
  result = newOrPrevType(tyIterable, prev, c)
  if n.len == 2:
    let base = semTypeNode(c, n[1], nil)
    addSonSkipIntLit(result, base, c.idgen)
  else:
    localError(c.config, n.info, errXExpectsOneTypeParam % "iterable")
    result = newOrPrevType(tyError, prev, c)

proc semOrdinal(c: PContext, n: PNode, prev: PType): PType =
  result = newOrPrevType(tyOrdinal, prev, c)
  if n.len == 2:
    var base = semTypeNode(c, n[1], nil)
    if base.kind != tyGenericParam:
      if not isOrdinalType(base):
        localError(c.config, n[1].info, errOrdinalTypeExpected % typeToString(base, preferDesc))
    addSonSkipIntLit(result, base, c.idgen)
  else:
    localError(c.config, n.info, errXExpectsOneTypeParam % "ordinal")
    result = newOrPrevType(tyError, prev, c)

proc semAnonTuple(c: PContext, n: PNode, prev: PType): PType =
  if n.len == 0:
    localError(c.config, n.info, errTypeExpected)
  result = newOrPrevType(tyTuple, prev, c)
  for it in n:
    let t = semTypeNode(c, it, nil)
    addSonSkipIntLitChecked(c, result, t, it, c.idgen)

proc semTuple(c: PContext, n: PNode, prev: PType): PType =
  var typ: PType
  result = newOrPrevType(tyTuple, prev, c)
  result.n = newNodeI(nkRecList, n.info)
  var check = initIntSet()
  var counter = 0
  for i in ord(n.kind == nkBracketExpr)..<n.len:
    var a = n[i]
    if (a.kind != nkIdentDefs): illFormedAst(a, c.config)
    checkMinSonsLen(a, 3, c.config)
    var hasDefaultField = a[^1].kind != nkEmpty
    if hasDefaultField:
      typ = fitDefaultNode(c, a)
    elif a[^2].kind != nkEmpty:
      typ = semTypeNode(c, a[^2], nil)
      if c.graph.config.isDefined("nimPreviewRangeDefault") and typ.skipTypes(abstractInst).kind == tyRange:
        a[^1] = newIntNode(nkIntLit, firstOrd(c.config, typ))
        a[^1].typ = typ
        hasDefaultField = true
    else:
      localError(c.config, a.info, errTypeExpected)
      typ = errorType(c)
    for j in 0..<a.len - 2:
      var field = newSymG(skField, a[j], c)
      field.typ = typ
      field.position = counter
      inc(counter)
      if containsOrIncl(check, field.name.id):
        localError(c.config, a[j].info, "attempt to redefine: '" & field.name.s & "'")
      else:
        let fSym = newSymNode(field)
        if hasDefaultField:
          fSym.sym.ast = a[^1]
          fSym.sym.ast.flags.incl nfSkipFieldChecking
        result.n.add fSym
        addSonSkipIntLit(result, typ, c.idgen)
      styleCheckDef(c, a[j].info, field)
      onDef(field.info, field)
  if result.n.len == 0: result.n = nil
  if isTupleRecursive(result):
    localError(c.config, n.info, errIllegalRecursionInTypeX % typeToString(result))

proc semIdentVis(c: PContext, kind: TSymKind, n: PNode,
                 allowed: TSymFlags): PSym =
  # identifier with visibility
  if n.kind == nkPostfix:
    if n.len == 2:
      # 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[1], c)
      var v = considerQuotedIdent(c, n[0])
      if sfExported in allowed and v.id == ord(wStar):
        incl(result.flags, sfExported)
      else:
        if not (sfExported in allowed):
          localError(c.config, n[0].info, errXOnlyAtModuleScope % "export")
        else:
          localError(c.config, n[0].info, errInvalidVisibilityX % renderTree(n[0]))
    else:
      illFormedAst(n, c.config)
  else:
    result = newSymG(kind, n, c)

proc semIdentWithPragma(c: PContext, kind: TSymKind, n: PNode,
                        allowed: TSymFlags): PSym =
  if n.kind == nkPragmaExpr:
    checkSonsLen(n, 2, c.config)
    result = semIdentVis(c, kind, n[0], allowed)
    case kind
    of skType:
      # process pragmas later, because result.typ has not been set yet
      discard
    of skField: pragma(c, result, n[1], fieldPragmas)
    of skVar:   pragma(c, result, n[1], varPragmas)
    of skLet:   pragma(c, result, n[1], letPragmas)
    of skConst: pragma(c, result, n[1], constPragmas)
    else: discard
  else:
    result = semIdentVis(c, kind, n, allowed)

proc checkForOverlap(c: PContext, t: PNode, currentEx, branchIndex: int) =
  let ex = t[branchIndex][currentEx].skipConv
  for i in 1..branchIndex:
    for j in 0..<t[i].len - 1:
      if i == branchIndex and j == currentEx: break
      if overlap(t[i][j].skipConv, ex):
        localError(c.config, ex.info, errDuplicateCaseLabel)

proc semBranchRange(c: PContext, t, a, b: PNode, covered: var Int128): PNode =
  checkMinSonsLen(t, 1, c.config)
  let ac = semConstExpr(c, a)
  let bc = semConstExpr(c, b)
  if ac.kind in {nkStrLit..nkTripleStrLit} or bc.kind in {nkStrLit..nkTripleStrLit}:
    localError(c.config, b.info, "range of string is invalid")
  let at = fitNode(c, t[0].typ, ac, ac.info).skipConvTakeType
  let bt = fitNode(c, t[0].typ, bc, bc.info).skipConvTakeType

  result = newNodeI(nkRange, a.info)
  result.add(at)
  result.add(bt)
  if emptyRange(ac, bc): localError(c.config, b.info, "range is empty")
  else: covered = covered + getOrdValue(bc) + 1 - getOrdValue(ac)

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

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

proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int,
                   covered: var Int128) =
  let lastIndex = branch.len - 2
  for i in 0..lastIndex:
    var b = branch[i]
    if b.kind == nkRange:
      branch[i] = b
    elif isRange(b):
      branch[i] = semCaseBranchRange(c, t, b, covered)
    else:
      # constant sets and arrays are allowed:
      # set expected type to selector type for type inference
      # even if it can be a different type like a set or array
      var r = semConstExpr(c, b, expectedType = t[0].typ)
      if r.kind in {nkCurly, nkBracket} and r.len == 0 and branch.len == 2:
        # discarding ``{}`` and ``[]`` branches silently
        delSon(branch, 0)
        return
      elif r.kind notin {nkCurly, nkBracket} or r.len == 0:
        checkMinSonsLen(t, 1, c.config)
        var tmp = fitNode(c, t[0].typ, r, r.info)
        # the call to fitNode may introduce a call to a converter
        if tmp.kind == nkHiddenCallConv or
            (tmp.kind == nkHiddenStdConv and t[0].typ.kind == tyCstring):
          tmp = semConstExpr(c, tmp)
        branch[i] = skipConv(tmp)
        inc(covered)
      else:
        if r.kind == nkCurly:
          r = deduplicate(c.config, r)

        # first element is special and will overwrite: branch[i]:
        branch[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:
          swap(branch[^2], branch[^1])
    checkForOverlap(c, t, i, branchIndex)

  # Elements added above needs to be checked for overlaps.
  for i in lastIndex.succ..<branch.len - 1:
    checkForOverlap(c, t, i, branchIndex)

proc toCover(c: PContext, t: PType): Int128 =
  let t2 = skipTypes(t, abstractVarRange-{tyTypeDesc})
  if t2.kind == tyEnum and enumHasHoles(t2):
    result = toInt128(t2.n.len)
  else:
    # <----
    let t = skipTypes(t, abstractVar-{tyTypeDesc})
    # XXX: hack incoming. lengthOrd is incorrect for 64bit integer
    # types because it doesn't uset Int128 yet.  This entire branching
    # should be removed as soon as lengthOrd uses int128.
    if t.kind in {tyInt64, tyUInt64}:
      result = toInt128(1) shl 64
    elif t.kind in {tyInt, tyUInt}:
      result = toInt128(1) shl (c.config.target.intSize * 8)
    else:
      result = lengthOrd(c.config, t)

proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
                      father: PNode, rectype: PType, hasCaseFields = false)

proc getIntSetOfType(c: PContext, t: PType): IntSet =
  result = initIntSet()
  if t.enumHasHoles:
    let t = t.skipTypes(abstractRange)
    for field in t.n.sons:
      result.incl(field.sym.position)
  else:
    assert(lengthOrd(c.config, t) <= BiggestInt(MaxSetElements))
    for i in toInt64(firstOrd(c.config, t))..toInt64(lastOrd(c.config, t)):
      result.incl(i.int)

iterator processBranchVals(b: PNode): int =
  assert b.kind in {nkOfBranch, nkElifBranch, nkElse}
  if b.kind == nkOfBranch:
    for i in 0..<b.len-1:
      if b[i].kind in {nkIntLit, nkCharLit}:
        yield b[i].intVal.int
      elif b[i].kind == nkRange:
        for i in b[i][0].intVal..b[i][1].intVal:
          yield i.int

proc renderAsType(vals: IntSet, t: PType): string =
  result = "{"
  let t = t.skipTypes(abstractRange)
  var enumSymOffset = 0
  var i = 0
  for val in vals:
    if result.len > 1:
      result &= ", "
    case t.kind:
    of tyEnum, tyBool:
      while t.n[enumSymOffset].sym.position < val: inc(enumSymOffset)
      result &= t.n[enumSymOffset].sym.name.s
    of tyChar:
      result.addQuoted(char(val))
    else:
      if i == 64:
        result &= "omitted $1 values..." % $(vals.len - i)
        break
      else:
        result &= $val
    inc(i)
  result &= "}"

proc formatMissingEnums(c: PContext, n: PNode): string =
  var coveredCases = initIntSet()
  for i in 1..<n.len:
    for val in processBranchVals(n[i]):
      coveredCases.incl val
  result = (c.getIntSetOfType(n[0].typ) - coveredCases).renderAsType(n[0].typ)

proc semRecordCase(c: PContext, n: PNode, check: var IntSet, pos: var int,
                   father: PNode, rectype: PType) =
  var a = copyNode(n)
  checkMinSonsLen(n, 2, c.config)
  semRecordNodeAux(c, n[0], check, pos, a, rectype, hasCaseFields = true)
  if a[0].kind != nkSym:
    internalError(c.config, "semRecordCase: discriminant is no symbol")
    return
  incl(a[0].sym.flags, sfDiscriminant)
  var covered = toInt128(0)
  var chckCovered = false
  var typ = skipTypes(a[0].typ, abstractVar-{tyTypeDesc})
  const shouldChckCovered = {tyInt..tyInt64, tyChar, tyEnum, tyUInt..tyUInt32, tyBool}
  case typ.kind
  of shouldChckCovered:
    chckCovered = true
  of tyFloat..tyFloat128, tyError:
    discard
  of tyRange:
    if skipTypes(typ[0], abstractInst).kind in shouldChckCovered:
      chckCovered = true
  of tyForward:
    errorUndeclaredIdentifier(c, n[0].info, typ.sym.name.s)
  elif not isOrdinalType(typ):
    localError(c.config, n[0].info, "selector must be of an ordinal type, float")
  if firstOrd(c.config, typ) != 0:
    localError(c.config, n.info, "low(" & $a[0].sym.name.s &
                                     ") must be 0 for discriminant")
  elif lengthOrd(c.config, typ) > 0x00007FFF:
    localError(c.config, n.info, "len($1) must be less than 32768" % a[0].sym.name.s)

  for i in 1..<n.len:
    var b = copyTree(n[i])
    a.add b
    case n[i].kind
    of nkOfBranch:
      checkMinSonsLen(b, 2, c.config)
      semCaseBranch(c, a, b, i, covered)
    of nkElse:
      checkSonsLen(b, 1, c.config)
      if chckCovered and covered == toCover(c, a[0].typ):
        message(c.config, b.info, warnUnreachableElse)
      chckCovered = false
    else: illFormedAst(n, c.config)
    delSon(b, b.len - 1)
    semRecordNodeAux(c, lastSon(n[i]), check, pos, b, rectype, hasCaseFields = true)
  if chckCovered and covered != toCover(c, a[0].typ):
    if a[0].typ.skipTypes(abstractRange).kind == tyEnum:
      localError(c.config, a.info, "not all cases are covered; missing: $1" %
                 formatMissingEnums(c, a))
    else:
      localError(c.config, a.info, "not all cases are covered")
  father.add a

proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
                      father: PNode, rectype: PType, hasCaseFields: bool) =
  if n == nil: return
  case n.kind
  of nkRecWhen:
    var a = copyTree(n)
    var branch: PNode = nil   # the branch to take
    for i in 0..<a.len:
      var it = a[i]
      if it == nil: illFormedAst(n, c.config)
      var idx = 1
      case it.kind
      of nkElifBranch:
        checkSonsLen(it, 2, c.config)
        if c.inGenericContext == 0:
          var e = semConstBoolExpr(c, it[0])
          if e.kind != nkIntLit: discard "don't report followup error"
          elif e.intVal != 0 and branch == nil: branch = it[1]
        else:
          it[0] = forceBool(c, semExprWithType(c, it[0]))
      of nkElse:
        checkSonsLen(it, 1, c.config)
        if branch == nil: branch = it[0]
        idx = 0
      else: illFormedAst(n, c.config)
      if c.inGenericContext > 0:
        # use a new check intset here for each branch:
        var newCheck: IntSet = check
        var newPos = pos
        var newf = newNodeI(nkRecList, n.info)
        semRecordNodeAux(c, it[idx], newCheck, newPos, newf, rectype, hasCaseFields)
        it[idx] = if newf.len == 1: newf[0] else: newf
    if c.inGenericContext > 0:
      father.add a
    elif branch != nil:
      semRecordNodeAux(c, branch, check, pos, father, rectype, hasCaseFields)
    elif father.kind in {nkElse, nkOfBranch}:
      father.add newNodeI(nkRecList, n.info)
  of nkRecCase:
    semRecordCase(c, n, check, pos, father, rectype)
  of nkNilLit:
    if father.kind != nkRecList: father.add 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 0..<n.len:
      semRecordNodeAux(c, n[i], check, pos, a, rectype, hasCaseFields)
    if a != father: father.add a
  of nkIdentDefs:
    checkMinSonsLen(n, 3, c.config)
    var a: PNode
    if father.kind != nkRecList and n.len >= 4: a = newNodeI(nkRecList, n.info)
    else: a = newNodeI(nkEmpty, n.info)
    var typ: PType
    var hasDefaultField = n[^1].kind != nkEmpty
    if hasDefaultField:
      typ = fitDefaultNode(c, n)
      propagateToOwner(rectype, typ)
    elif n[^2].kind == nkEmpty:
      localError(c.config, n.info, errTypeExpected)
      typ = errorType(c)
    else:
      typ = semTypeNode(c, n[^2], nil)
      if c.graph.config.isDefined("nimPreviewRangeDefault") and typ.skipTypes(abstractInst).kind == tyRange:
        n[^1] = newIntNode(nkIntLit, firstOrd(c.config, typ))
        n[^1].typ = typ
        hasDefaultField = true
      propagateToOwner(rectype, typ)
    var fieldOwner = if c.inGenericContext > 0: c.getCurrOwner
                     else: rectype.sym
    for i in 0..<n.len-2:
      var f = semIdentWithPragma(c, skField, n[i], {sfExported})
      let info = if n[i].kind == nkPostfix:
                   n[i][1].info
                 else:
                   n[i].info
      suggestSym(c.graph, info, f, c.graph.usageSym)
      f.typ = typ
      f.position = pos
      f.options = c.config.options
      if fieldOwner != nil and
         {sfImportc, sfExportc} * fieldOwner.flags != {} and
         not hasCaseFields and f.loc.r == "":
        f.loc.r = rope(f.name.s)
        f.flags.incl {sfImportc, sfExportc} * fieldOwner.flags
      inc(pos)
      if containsOrIncl(check, f.name.id):
        localError(c.config, info, "attempt to redefine: '" & f.name.s & "'")
      let fSym = newSymNode(f)
      if hasDefaultField:
        fSym.sym.ast = n[^1]
        fSym.sym.ast.flags.incl nfSkipFieldChecking
      if a.kind == nkEmpty: father.add fSym
      else: a.add fSym
      styleCheckDef(c, f)
      onDef(f.info, f)
    if a.kind != nkEmpty: father.add a
  of nkSym:
    # This branch only valid during generic object
    # inherited from generic/partial specialized parent second check.
    # There is no branch validity check here
    if containsOrIncl(check, n.sym.name.id):
      localError(c.config, n.info, "attempt to redefine: '" & n.sym.name.s & "'")
    father.add n
  of nkEmpty:
    if father.kind in {nkElse, nkOfBranch}:
      father.add n
  else: illFormedAst(n, c.config)

proc addInheritedFieldsAux(c: PContext, check: var IntSet, pos: var int,
                           n: PNode) =
  case n.kind
  of nkRecCase:
    if (n[0].kind != nkSym): internalError(c.config, n.info, "addInheritedFieldsAux")
    addInheritedFieldsAux(c, check, pos, n[0])
    for i in 1..<n.len:
      case n[i].kind
      of nkOfBranch, nkElse:
        addInheritedFieldsAux(c, check, pos, lastSon(n[i]))
      else: internalError(c.config, n.info, "addInheritedFieldsAux(record case branch)")
  of nkRecList, nkRecWhen, nkElifBranch, nkElse:
    for i in int(n.kind == nkElifBranch)..<n.len:
      addInheritedFieldsAux(c, check, pos, n[i])
  of nkSym:
    incl(check, n.sym.name.id)
    inc(pos)
  else: internalError(c.config, n.info, "addInheritedFieldsAux()")

proc skipGenericInvocation(t: PType): PType {.inline.} =
  result = t
  if result.kind == tyGenericInvocation:
    result = result[0]
  while result.kind in {tyGenericInst, tyGenericBody, tyRef, tyPtr, tyAlias, tySink, tyOwned}:
    result = lastSon(result)

proc tryAddInheritedFields(c: PContext, check: var IntSet, pos: var int,
                        obj: PType, n: PNode, isPartial = false): bool =
  if (not isPartial) and (obj.kind notin {tyObject, tyGenericParam} or tfFinal in obj.flags):
    localError(c.config, n.info, "Cannot inherit from: '" & $obj & "'")
    result = false
  elif obj.kind == tyObject:
    result = true
    if (obj.len > 0) and (obj[0] != nil):
      result = result and tryAddInheritedFields(c, check, pos, obj[0].skipGenericInvocation, n)
    addInheritedFieldsAux(c, check, pos, obj.n)
  else:
    result = true

proc semObjectNode(c: PContext, n: PNode, prev: PType; flags: TTypeFlags): PType =
  if n.len == 0:
    return newConstraint(c, tyObject)
  var check = initIntSet()
  var pos = 0
  var base, realBase: PType = nil
  # n[0] contains the pragmas (if any). We process these later...
  checkSonsLen(n, 3, c.config)
  if n[1].kind != nkEmpty:
    realBase = semTypeNode(c, n[1][0], nil)
    base = skipTypesOrNil(realBase, skipPtrs)
    if base.isNil:
      localError(c.config, n.info, "cannot inherit from a type that is not an object type")
    else:
      var concreteBase = skipGenericInvocation(base)
      if concreteBase.kind in {tyObject, tyGenericParam,
        tyGenericInvocation} and tfFinal notin concreteBase.flags:
        # we only check fields duplication of object inherited from
        # concrete object. If inheriting from generic object or partial
        # specialized object, there will be second check after instantiation
        # located in semGeneric.
        if concreteBase.kind == tyObject:
          if concreteBase.sym != nil and concreteBase.sym.magic == mException and
              sfSystemModule notin c.module.flags:
            message(c.config, n.info, warnInheritFromException, "")
          if not tryAddInheritedFields(c, check, pos, concreteBase, n):
            return newType(tyError, nextTypeId c.idgen, result.owner)

      else:
        if concreteBase.kind != tyError:
          localError(c.config, n[1].info, "inheritance only works with non-final objects; " &
             "for " & typeToString(realBase) & " to be inheritable it must be " &
             "'object of RootObj' instead of 'object'")
        base = nil
        realBase = nil
  if n.kind != nkObjectTy: internalError(c.config, n.info, "semObjectNode")
  result = newOrPrevType(tyObject, prev, c)
  rawAddSon(result, realBase)
  if realBase == nil and tfInheritable in flags:
    result.flags.incl tfInheritable
  if tfAcyclic in flags: result.flags.incl tfAcyclic
  if result.n.isNil:
    result.n = newNodeI(nkRecList, n.info)
  else:
    # partial object so add things to the check
    if not tryAddInheritedFields(c, check, pos, result, n, isPartial = true):
      return newType(tyError, nextTypeId c.idgen, result.owner)

  semRecordNodeAux(c, n[2], check, pos, result.n, result)
  if n[0].kind != nkEmpty:
    # dummy symbol for `pragma`:
    var s = newSymS(skType, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
    s.typ = result
    pragma(c, s, n[0], typePragmas)
  if base == nil and tfInheritable notin result.flags:
    incl(result.flags, tfFinal)
  if c.inGenericContext == 0 and computeRequiresInit(c, result):
    result.flags.incl tfRequiresInit

proc semAnyRef(c: PContext; n: PNode; kind: TTypeKind; prev: PType): PType =
  if n.len < 1:
    result = newConstraint(c, kind)
  else:
    let isCall = int ord(n.kind in nkCallKinds+{nkBracketExpr})
    let n = if n[0].kind == nkBracket: n[0] else: n
    checkMinSonsLen(n, 1, c.config)
    let body = n.lastSon
    var t = if prev != nil and prev.kind != tyGenericBody and body.kind == nkObjectTy:
              semObjectNode(c, body, nil, prev.flags)
            else:
              semTypeNode(c, body, nil)
    if t.kind == tyTypeDesc and tfUnresolved notin t.flags:
      t = t.base
    if t.kind == tyVoid:
      localError(c.config, n.info, "type '$1 void' is not allowed" % kind.toHumanStr)
    result = newOrPrevType(kind, prev, c)
    var isNilable = false
    var wrapperKind = tyNone
    # check every except the last is an object:
    for i in isCall..<n.len-1:
      let ni = n[i]
      # echo "semAnyRef ", "n: ", n, "i: ", i, "ni: ", ni
      if ni.kind == nkNilLit:
        isNilable = true
      else:
        let region = semTypeNode(c, ni, nil)
        if region.kind in {tyOwned, tySink}:
          wrapperKind = region.kind
        elif region.skipTypes({tyGenericInst, tyAlias, tySink}).kind notin {
              tyError, tyObject}:
          message c.config, n[i].info, errGenerated, "region needs to be an object type"
          addSonSkipIntLit(result, region, c.idgen)
        else:
          message(c.config, n.info, warnDeprecated, "region for pointer types is deprecated")
          addSonSkipIntLit(result, region, c.idgen)
    addSonSkipIntLit(result, t, c.idgen)
    if tfPartial in result.flags:
      if result.lastSon.kind == tyObject: incl(result.lastSon.flags, tfPartial)
    # if not isNilable: result.flags.incl tfNotNil
    case wrapperKind
    of tyOwned:
      if optOwnedRefs in c.config.globalOptions:
        let t = newTypeS(tyOwned, c)
        t.flags.incl tfHasOwned
        t.rawAddSonNoPropagationOfTypeFlags result
        result = t
    of tySink:
      let t = newTypeS(tySink, c)
      t.rawAddSonNoPropagationOfTypeFlags result
      result = t
    else: discard
    if result.kind == tyRef and c.config.selectedGC in {gcArc, gcOrc, gcAtomicArc}:
      result.flags.incl tfHasAsgn

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 == nil: return nil
  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) =
  if kind == skMacro:
    let staticType = findEnforcedStaticType(param.typ)
    if staticType != nil:
      var a = copySym(param, c.idgen)
      a.typ = staticType.base
      addDecl(c, a)
      #elif param.typ != nil and param.typ.kind == tyTypeDesc:
      #  addDecl(c, param)
    else:
      # within a macro, every param has the type NimNode!
      let nn = getSysSym(c.graph, param.info, "NimNode")
      var a = copySym(param, c.idgen)
      a.typ = nn.typ
      addDecl(c, a)
  else:
    if sfGenSym in param.flags:
      # bug #XXX, fix the gensym'ed parameters owner:
      if param.owner == nil:
        param.owner = getCurrOwner(c)
    else: addDecl(c, param)

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

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

  let owner = if typeClass.sym != nil: typeClass.sym
              else: getCurrOwner(c)
  var s = newSym(skType, finalTypId, c.idgen, owner, info)
  if sfExplain in owner.flags: s.flags.incl sfExplain
  if typId == nil: s.flags.incl(sfAnon)
  s.linkTo(typeClass)
  typeClass.flags.incl tfImplicitTypeParam
  s.position = genericParams.len
  genericParams.add newSymNode(s)
  result = typeClass
  addDecl(c, s)

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)

  template recurse(typ: PType, anonFlag = false): untyped =
    liftParamType(c, procKind, genericParams, typ, paramName, info, anonFlag)

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

  case paramType.kind
  of tyAnything:
    result = addImplicitGeneric(c, newTypeS(tyGenericParam, c), nil, info, genericParams, paramName)

  of tyStatic:
    if paramType.base.kind != tyNone and paramType.n != nil:
      # this is a concrete static value
      return
    if tfUnresolved in paramType.flags: return # already lifted

    let lifted = recurse(paramType.base)
    let base = (if lifted != nil: lifted else: paramType.base)
    if base.isMetaType and procKind == skMacro:
      localError(c.config, info, errMacroBodyDependsOnGenericTypes % paramName)
    result = addImplicitGeneric(c, c.newTypeWithSons(tyStatic, @[base]),
        paramTypId, info, genericParams, paramName)
    if result != nil: 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 == getIdent(c.cache, "typedesc").id or
          paramTypId.id == getIdent(c.cache, "type").id):
        # XXX Why doesn't this check for tyTypeDesc instead?
        paramTypId = nil
      let t = c.newTypeWithSons(tyTypeDesc, @[paramType.base])
      incl t.flags, tfCheckedForDestructor
      result = addImplicitGeneric(c, t, paramTypId, info, genericParams, paramName)

  of tyDistinct:
    if paramType.len == 1:
      # disable the bindOnce behavior for the type class
      result = recurse(paramType.base, true)

  of tyTuple:
    for i in 0..<paramType.len:
      let t = recurse(paramType[i])
      if t != nil:
        paramType[i] = t
        result = paramType

  of tyAlias, tyOwned, tySink:
    result = recurse(paramType.base)

  of tySequence, tySet, tyArray, tyOpenArray,
     tyVar, tyLent, 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(c, typ, paramTypId, info, genericParams, paramName)
    else:
      for i in 0..<paramType.len:
        if paramType[i] == paramType:
          globalError(c.config, info, errIllegalRecursionInTypeX % typeToString(paramType))
        var lifted = recurse(paramType[i])
        if lifted != nil:
          paramType[i] = lifted
          result = paramType

  of tyGenericBody:
    result = newTypeS(tyGenericInvocation, c)
    result.rawAddSon(paramType)

    for i in 0..<paramType.len - 1:
      if paramType[i].kind == tyStatic:
        var staticCopy = paramType[i].exactReplica
        staticCopy.flags.incl tfInferrableStatic
        result.rawAddSon staticCopy
      else:
        result.rawAddSon newTypeS(tyAnything, c)

    if paramType.lastSon.kind == tyUserTypeClass:
      result.kind = tyUserTypeClassInst
      result.rawAddSon paramType.lastSon
      return addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)

    let x = instGenericContainer(c, paramType.sym.info, result,
                                  allowMetaTypes = true)
    result = newTypeWithSons(c, tyCompositeTypeClass, @[paramType, x])
    #result = newTypeS(tyCompositeTypeClass, c)
    #for i in 0..<x.len: result.rawAddSon(x[i])
    result = addImplicitGeneric(c, result, paramTypId, info, genericParams, paramName)

  of tyGenericInst:
    if paramType.lastSon.kind == tyUserTypeClass:
      var cp = copyType(paramType, nextTypeId c.idgen, getCurrOwner(c))
      copyTypeProps(c.graph, c.idgen.module, cp, paramType)

      cp.kind = tyUserTypeClassInst
      return addImplicitGeneric(c, cp, paramTypId, info, genericParams, paramName)

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

    let liftBody = recurse(paramType.lastSon, true)
    if liftBody != nil:
      result = liftBody
      result.flags.incl tfHasMeta
      #result.shouldHaveMeta

  of tyGenericInvocation:
    for i in 1..<paramType.len:
      #if paramType[i].kind != tyTypeDesc:
      let lifted = recurse(paramType[i])
      if lifted != nil: paramType[i] = lifted

    let body = paramType.base
    if body.kind in {tyForward, tyError}:
      # this may happen for proc type appearing in a type section
      # before one of its param types
      return

    if body.lastSon.kind == tyUserTypeClass:
      let expanded = instGenericContainer(c, info, paramType,
                                          allowMetaTypes = true)
      result = recurse(expanded, true)

  of tyUserTypeClasses, tyBuiltInTypeClass, tyCompositeTypeClass,
     tyAnd, tyOr, tyNot, tyConcept:
    result = addImplicitGeneric(c,
        copyType(paramType, nextTypeId c.idgen, getCurrOwner(c)), paramTypId,
        info, genericParams, paramName)

  of tyGenericParam:
    markUsed(c, paramType.sym.info, paramType.sym)
    onUse(paramType.sym.info, paramType.sym)
    if tfWildcard in paramType.flags:
      paramType.flags.excl tfWildcard
      paramType.sym.transitionGenericParamToType()

  else: discard

proc semParamType(c: PContext, n: PNode, constraint: var PNode): PType =
  ## Semchecks the type of parameters.
  if n.kind == nkCurlyExpr:
    result = semTypeNode(c, n[0], nil)
    constraint = semNodeKindConstraints(n, c.config, 1)
  elif n.kind == nkCall and
      n[0].kind in {nkIdent, nkSym, nkOpenSymChoice, nkClosedSymChoice} and
      considerQuotedIdent(c, n[0]).s == "{}":
    result = semTypeNode(c, n[1], nil)
    constraint = semNodeKindConstraints(n, c.config, 2)
  else:
    result = semTypeNode(c, n, nil)

proc newProcType(c: PContext; info: TLineInfo; prev: PType = nil): PType =
  result = newOrPrevType(tyProc, prev, c)
  result.callConv = lastOptionEntry(c).defaultCC
  result.n = newNodeI(nkFormalParams, info)
  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:
  result.n.add newNodeI(nkEffectList, info)

proc isMagic(sym: PSym): bool =
  if sym.ast == nil: return false
  let nPragmas = sym.ast[pragmasPos]
  return hasPragma(nPragmas, wMagic)

proc semProcTypeNode(c: PContext, n, genericParams: PNode,
                     prev: PType, kind: TSymKind; isType=false): PType =
  # for historical reasons (code grows) this is invoked for parameter
  # lists too and then 'isType' is false.
  checkMinSonsLen(n, 1, c.config)
  result = newProcType(c, n.info, prev)
  var check = initIntSet()
  var counter = 0

  for i in 1..<n.len:
    var a = n[i]
    if a.kind != nkIdentDefs:
      # for some generic instantiations the passed ':env' parameter
      # for closures has already been produced (see bug #898). We simply
      # skip this parameter here. It'll then be re-generated in another LL
      # pass over this instantiation:
      if a.kind == nkSym and sfFromGeneric in a.sym.flags: continue
      illFormedAst(a, c.config)

    checkMinSonsLen(a, 3, c.config)
    var
      typ: PType = nil
      def: PNode = nil
      constraint: PNode = nil
      hasType = a[^2].kind != nkEmpty
      hasDefault = a[^1].kind != nkEmpty

    if hasType:
      typ = semParamType(c, a[^2], constraint)
      # TODO: Disallow typed/untyped in procs in the compiler/stdlib
      if kind in {skProc, skFunc} and (typ.kind == tyTyped or typ.kind == tyUntyped):
        if not isMagic(getCurrOwner(c)):
          localError(c.config, a[^2].info, "'" & typ.sym.name.s & "' is only allowed in templates and macros or magic procs")


    if hasDefault:
      def = a[^1]
      if a.len > 3:
        var msg = ""
        for j in 0 ..< a.len - 2:
          if msg.len != 0: msg.add(", ")
          msg.add($a[j])
        msg.add(" all have default value '")
        msg.add(def.renderTree)
        msg.add("', this may be unintentional, " &
          "either use ';' (semicolon) or explicitly write each default value")
        message(c.config, a.info, warnImplicitDefaultValue, msg)
      block determineType:
        var defTyp = typ
        if genericParams != nil and genericParams.len > 0:
          defTyp = nil
          def = semGenericStmt(c, def)
          if hasUnresolvedArgs(c, def):
            def.typ = makeTypeFromExpr(c, def.copyTree)
            break determineType

        def = semExprWithType(c, def, {efDetermineType}, defTyp)
        if def.referencesAnotherParam(getCurrOwner(c)):
          def.flags.incl nfDefaultRefsParam

      if typ == nil:
        typ = def.typ
        if isEmptyContainer(typ):
          localError(c.config, a.info, "cannot infer the type of parameter '" & $a[0] & "'")

        if typ.kind == tyTypeDesc:
          # consider a proc such as:
          # proc takesType(T = int)
          # a naive analysis may conclude that the proc type is type[int]
          # which will prevent other types from matching - clearly a very
          # surprising behavior. We must instead fix the expected type of
          # the proc to be the unbound typedesc type:
          typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
          typ.flags.incl tfCheckedForDestructor

      else:
        # if 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):
          # check type compatibility between def.typ and typ:
          def = fitNode(c, typ, def, def.info)
        elif typ.kind == tyStatic:
          def = semConstExpr(c, def)
          def = fitNode(c, typ, def, def.info)

    if not hasType and not hasDefault:
      if isType: localError(c.config, a.info, "':' expected")
      if kind in {skTemplate, skMacro}:
        typ = newTypeS(tyUntyped, c)
    elif skipTypes(typ, {tyGenericInst, tyAlias, tySink}).kind == tyVoid:
      continue

    for j in 0..<a.len-2:
      var arg = newSymG(skParam, if a[j].kind == nkPragmaExpr: a[j][0] else: a[j], c)
      if arg.name.id == ord(wUnderscore):
        arg.flags.incl(sfGenSym)
      elif containsOrIncl(check, arg.name.id):
        localError(c.config, a[j].info, "attempt to redefine: '" & arg.name.s & "'")
      if a[j].kind == nkPragmaExpr:
        pragma(c, arg, a[j][1], paramPragmas)
      if not hasType and not hasDefault and kind notin {skTemplate, skMacro}:
        let param = strTableGet(c.signatures, arg.name)
        if param != nil: typ = param.typ
        else:
          localError(c.config, a.info, "parameter '$1' requires a type" % arg.name.s)
          typ = errorType(c)
      var nameForLift = arg.name.s
      if sfGenSym in arg.flags:
        nameForLift.add("`gensym" & $arg.id)
      let lifted = liftParamType(c, kind, genericParams, typ,
                                 nameForLift, arg.info)
      let finalType = if lifted != nil: lifted else: typ.skipIntLit(c.idgen)
      arg.typ = finalType
      arg.position = counter
      arg.constraint = constraint
      inc(counter)
      if def != nil and def.kind != nkEmpty:
        arg.ast = copyTree(def)
      result.n.add newSymNode(arg)
      rawAddSon(result, finalType)
      addParamOrResult(c, arg, kind)
      styleCheckDef(c, a[j].info, arg)
      onDef(a[j].info, arg)
      a[j] = newSymNode(arg)

  var r: PType
  if n[0].kind != nkEmpty:
    r = semTypeNode(c, n[0], nil)

  if r != nil and kind in {skMacro, skTemplate} and r.kind == tyTyped:
    # XXX: To implement the proposed change in the warning, just
    # delete this entire if block. The rest is (at least at time of
    # writing this comment) already implemented.
    let info = n[0].info
    const msg = "`typed` will change its meaning in future versions of Nim. " &
                "`void` or no return type declaration at all has the same " &
                "meaning as the current meaning of `typed` as return type " &
                "declaration."
    message(c.config, info, warnDeprecated, msg)
    r = nil

  if r != nil:
    # turn explicit 'void' return type into 'nil' because the rest of the
    # compiler only checks for 'nil':
    if skipTypes(r, {tyGenericInst, tyAlias, tySink}).kind != tyVoid:
      if kind notin {skMacro, skTemplate} and r.kind in {tyTyped, tyUntyped}:
        localError(c.config, n[0].info, "return type '" & typeToString(r) &
            "' is only valid for macros and templates")
      # 'auto' as a return type does not imply a generic:
      elif r.kind == tyAnything:
        discard
      elif r.kind == tyStatic:
        # type allowed should forbid this type
        discard
      else:
        if r.sym == nil or sfAnon notin r.sym.flags:
          let lifted = liftParamType(c, kind, genericParams, r, "result",
                                     n[0].info)
          if lifted != nil:
            r = lifted
            #if r.kind != tyGenericParam:
            #echo "came here for ", typeToString(r)
            r.flags.incl tfRetType
        r = skipIntLit(r, c.idgen)
        if kind == skIterator:
          # see tchainediterators
          # in cases like iterator foo(it: iterator): typeof(it)
          # we don't need to change the return type to iter[T]
          result.flags.incl tfIterator
          # XXX Would be nice if we could get rid of this
      result[0] = r
      let oldFlags = result.flags
      propagateToOwner(result, r)
      if oldFlags != result.flags:
        # XXX This rather hacky way keeps 'tflatmap' compiling:
        if tfHasMeta notin oldFlags:
          result.flags.excl tfHasMeta
      result.n.typ = r

  if genericParams != nil and genericParams.len > 0:
    for n in genericParams:
      if {sfUsed, sfAnon} * n.sym.flags == {}:
        result.flags.incl tfUnresolved

      if tfWildcard in n.sym.typ.flags:
        n.sym.transitionGenericParamToType()
        n.sym.typ.flags.excl tfWildcard

proc semStmtListType(c: PContext, n: PNode, prev: PType): PType =
  checkMinSonsLen(n, 1, c.config)
  for i in 0..<n.len - 1:
    n[i] = semStmt(c, n[i], {})
  if n.len > 0:
    result = semTypeNode(c, n[^1], prev)
    n.typ = result
    n[^1].typ = result
  else:
    result = nil

proc semBlockType(c: PContext, n: PNode, prev: PType): PType =
  inc(c.p.nestedBlockCounter)
  let oldBreakInLoop = c.p.breakInLoop
  c.p.breakInLoop = false
  checkSonsLen(n, 2, c.config)
  openScope(c)
  if n[0].kind notin {nkEmpty, nkSym}:
    addDecl(c, newSymS(skLabel, n[0], c))
  result = semStmtListType(c, n[1], prev)
  n[1].typ = result
  n.typ = result
  closeScope(c)
  c.p.breakInLoop = oldBreakInLoop
  dec(c.p.nestedBlockCounter)

proc semGenericParamInInvocation(c: PContext, n: PNode): PType =
  result = semTypeNode(c, n, nil)
  n.typ = makeTypeDesc(c, result)

proc trySemObjectTypeForInheritedGenericInst(c: PContext, n: PNode, t: PType): bool =
  var
    check = initIntSet()
    pos = 0
  let
    realBase = t[0]
    base = skipTypesOrNil(realBase, skipPtrs)
  result = true
  if base.isNil:
    localError(c.config, n.info, errIllegalRecursionInTypeX % "object")
  else:
    let concreteBase = skipGenericInvocation(base)
    if concreteBase.kind == tyObject and tfFinal notin concreteBase.flags:
      if not tryAddInheritedFields(c, check, pos, concreteBase, n):
        return false
    else:
      if concreteBase.kind != tyError:
        localError(c.config, n.info, errInheritanceOnlyWithNonFinalObjects)
  var newf = newNodeI(nkRecList, n.info)
  semRecordNodeAux(c, t.n, check, pos, newf, t)

proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType =
  if s.typ == nil:
    localError(c.config, n.info, "cannot instantiate the '$1' $2" %
               [s.name.s, s.kind.toHumanStr])
    return newOrPrevType(tyError, prev, c)

  var t = s.typ.skipTypes({tyAlias})
  if t.kind == tyCompositeTypeClass and t.base.kind == tyGenericBody:
    t = t.base

  result = newOrPrevType(tyGenericInvocation, prev, c)
  addSonSkipIntLit(result, t, c.idgen)

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

  if t.kind == tyForward:
    for i in 1..<n.len:
      var elem = semGenericParamInInvocation(c, n[i])
      addToResult(elem)
    return
  elif t.kind != tyGenericBody:
    # we likely got code of the form TypeA[TypeB] where TypeA is
    # not generic.
    localError(c.config, n.info, errNoGenericParamsAllowedForX % s.name.s)
    return newOrPrevType(tyError, prev, c)
  else:
    var m = newCandidate(c, t)
    m.isNoCall = true
    matches(c, n, copyTree(n), m)

    if m.state != csMatch:
      var err = "cannot instantiate "
      err.addTypeHeader(c.config, t)
      err.add "\ngot: <$1>\nbut expected: <$2>" % [describeArgs(c, n), describeArgs(c, t.n, 0)]
      localError(c.config, n.info, errGenerated, err)
      return newOrPrevType(tyError, prev, c)

    var isConcrete = true

    for i in 1..<m.call.len:
      var typ = m.call[i].typ
      # is this a 'typedesc' *parameter*? If so, use the typedesc type,
      # unstripped.
      if m.call[i].kind == nkSym and m.call[i].sym.kind == skParam and
          typ.kind == tyTypeDesc and containsGenericType(typ):
        isConcrete = false
        addToResult(typ)
      else:
        typ = 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(c.config, n.info, errCannotInstantiateX % s.name.s)
        result = newOrPrevType(tyError, prev, c)
      else:
        result = instGenericContainer(c, n.info, result,
                                      allowMetaTypes = false)

  # special check for generic object with
  # generic/partial specialized parent
  let tx = result.skipTypes(abstractPtrs, 50)
  if tx.isNil or isTupleRecursive(tx):
    localError(c.config, n.info, "illegal recursion in type '$1'" % typeToString(result[0]))
    return errorType(c)
  if tx != result and tx.kind == tyObject:
    if tx[0] != nil:
      if not trySemObjectTypeForInheritedGenericInst(c, n, tx):
        return newOrPrevType(tyError, prev, c)
    var position = 0
    recomputeFieldPositions(tx, tx.n, position)

proc maybeAliasType(c: PContext; typeExpr, prev: PType): PType =
  if typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward, tyGenericBody} and prev != nil:
    result = newTypeS(tyAlias, c)
    result.rawAddSon typeExpr
    result.sym = prev.sym
    if prev.kind != tyGenericBody:
      assignType(prev, result)

proc fixupTypeOf(c: PContext, prev: PType, typExpr: PNode) =
  if prev != nil:
    let result = newTypeS(tyAlias, c)
    result.rawAddSon typExpr.typ
    result.sym = prev.sym
    if prev.kind != tyGenericBody:
      assignType(prev, result)

proc semTypeExpr(c: PContext, n: PNode; prev: PType): PType =
  var n = semExprWithType(c, n, {efDetermineType})
  if n.typ.kind == tyTypeDesc:
    result = n.typ.base
    # fix types constructed by macros/template:
    if prev != nil and prev.kind != tyGenericBody and prev.sym != nil:
      if result.sym.isNil:
        # Behold! you're witnessing enormous power yielded
        # by macros. Only macros can summon unnamed types
        # and cast spell upon AST. Here we need to give
        # it a name taken from left hand side's node
        result.sym = prev.sym
        result.sym.typ = result
      else:
        # Less powerful routine like template do not have
        # the ability to produce unnamed types. But still
        # it has wild power to push a type a bit too far.
        # So we need to hold it back using alias and prevent
        # unnecessary new type creation
        let alias = maybeAliasType(c, result, prev)
        if alias != nil: result = alias
  else:
    localError(c.config, n.info, "expected type, but got: " & n.renderTree)
    result = errorType(c)

proc freshType(c: PContext; res, prev: PType): PType {.inline.} =
  if prev.isNil or prev.kind == tyGenericBody:
    result = copyType(res, nextTypeId c.idgen, res.owner)
    copyTypeProps(c.graph, c.idgen.module, result, res)
  else:
    result = res

template modifierTypeKindOfNode(n: PNode): TTypeKind =
  case n.kind
  of nkVarTy: tyVar
  of nkRefTy: tyRef
  of nkPtrTy: tyPtr
  of nkStaticTy: tyStatic
  of nkTypeOfExpr: tyTypeDesc
  else: tyNone

proc semTypeClass(c: PContext, n: PNode, prev: PType): PType =
  # if n.len == 0: return newConstraint(c, tyTypeClass)
  if isNewStyleConcept(n):
    result = newOrPrevType(tyConcept, prev, c)
    result.flags.incl tfCheckedForDestructor
    result.n = semConceptDeclaration(c, n)
    return result

  let
    pragmas = n[1]
    inherited = n[2]

  result = newOrPrevType(tyUserTypeClass, prev, c)
  result.flags.incl tfCheckedForDestructor
  var owner = getCurrOwner(c)
  var candidateTypeSlot = newTypeWithSons(owner, tyAlias, @[c.errorType], c.idgen)
  result.sons = @[candidateTypeSlot]
  result.n = n

  if inherited.kind != nkEmpty:
    for n in inherited.sons:
      let typ = semTypeNode(c, n, nil)
      result.add(typ)

  openScope(c)
  for param in n[0]:
    var
      dummyName: PNode
      dummyType: PType

    let modifier = param.modifierTypeKindOfNode

    if modifier != tyNone:
      dummyName = param[0]
      dummyType = c.makeTypeWithModifier(modifier, candidateTypeSlot)
      # if modifier == tyRef:
        # dummyType.flags.incl tfNotNil
      if modifier == tyTypeDesc:
        dummyType.flags.incl tfConceptMatchedTypeSym
        dummyType.flags.incl tfCheckedForDestructor
    else:
      dummyName = param
      dummyType = candidateTypeSlot

    # this can be true for 'nim check' on incomplete concepts,
    # see bug #8230
    if dummyName.kind == nkEmpty: continue

    internalAssert c.config, dummyName.kind == nkIdent
    var dummyParam = newSym(if modifier == tyTypeDesc: skType else: skVar,
                            dummyName.ident, c.idgen, owner, param.info)
    dummyParam.typ = dummyType
    incl dummyParam.flags, sfUsed
    addDecl(c, dummyParam)

  result.n[3] = semConceptBody(c, n[3])
  closeScope(c)

proc applyTypeSectionPragmas(c: PContext; pragmas, operand: PNode): PNode =
  for p in pragmas:
    let key = if p.kind in nkPragmaCallKinds and p.len >= 1: p[0] else: p

    if p.kind == nkEmpty or whichPragma(p) != wInvalid:
      discard "builtin pragma"
    else:
      let ident = considerQuotedIdent(c, key)
      if strTableGet(c.userPragmas, ident) != nil:
        discard "User-defined pragma"
      else:
        var amb = false
        let sym = searchInScopes(c, ident, amb)
        # XXX: What to do here if amb is true?
        if sym != nil and sfCustomPragma in sym.flags:
          discard "Custom user pragma"
        else:
          # we transform ``(arg1, arg2: T) {.m, rest.}`` into ``m((arg1, arg2: T) {.rest.})`` and
          # let the semantic checker deal with it:
          var x = newNodeI(nkCall, key.info)
          x.add(key)
          if p.kind in nkPragmaCallKinds and p.len > 1:
            # pass pragma arguments to the macro too:
            for i in 1 ..< p.len:
              x.add(p[i])
          # Also pass the node the pragma has been applied to
          x.add(operand.copyTreeWithoutNode(p))
          # recursion assures that this works for multiple macro annotations too:
          var r = semOverloadedCall(c, x, x, {skMacro, skTemplate}, {efNoUndeclared})
          if r != nil:
            doAssert r[0].kind == nkSym
            let m = r[0].sym
            case m.kind
            of skMacro: return semMacroExpr(c, r, r, m, {efNoSemCheck})
            of skTemplate: return semTemplateExpr(c, r, m, {efNoSemCheck})
            else: doAssert(false, "cannot happen")

proc semProcTypeWithScope(c: PContext, n: PNode,
                          prev: PType, kind: TSymKind): PType =
  checkSonsLen(n, 2, c.config)

  if n[1].kind != nkEmpty and n[1].len > 0:
    let macroEval = applyTypeSectionPragmas(c, n[1], n)
    if macroEval != nil:
      return semTypeNode(c, macroEval, prev)

  openScope(c)
  result = semProcTypeNode(c, n[0], nil, prev, kind, isType=true)
  # start with 'ccClosure', but of course pragmas can overwrite this:
  result.callConv = ccClosure
  # dummy symbol for `pragma`:
  var s = newSymS(kind, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
  s.typ = result
  if n[1].kind != nkEmpty and n[1].len > 0:
    pragma(c, s, n[1], procTypePragmas)
    when useEffectSystem: setEffectsForProcType(c.graph, result, n[1])
  elif c.optionStack.len > 0:
    # we construct a fake 'nkProcDef' for the 'mergePragmas' inside 'implicitPragmas'...
    s.ast = newTree(nkProcDef, newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info),
        newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info), newNodeI(nkEmpty, n.info))
    implicitPragmas(c, s, n.info, {wTags, wRaises})
    when useEffectSystem: setEffectsForProcType(c.graph, result, s.ast[pragmasPos])
  closeScope(c)

proc symFromExpectedTypeNode(c: PContext, n: PNode): PSym =
  if n.kind == nkType:
    result = symFromType(c, n.typ, n.info)
  else:
    localError(c.config, n.info, errTypeExpected)
    result = errorSym(c, n)

proc semStaticType(c: PContext, childNode: PNode, prev: PType): PType =
  result = newOrPrevType(tyStatic, prev, c)
  var base = semTypeNode(c, childNode, nil).skipTypes({tyTypeDesc, tyAlias})
  result.rawAddSon(base)
  result.flags.incl tfHasStatic

proc semTypeOf(c: PContext; n: PNode; prev: PType): PType =
  openScope(c)
  let t = semExprWithType(c, n, {efInTypeof})
  closeScope(c)
  fixupTypeOf(c, prev, t)
  result = t.typ

proc semTypeOf2(c: PContext; n: PNode; prev: PType): PType =
  openScope(c)
  var m = BiggestInt 1 # typeOfIter
  if n.len == 3:
    let mode = semConstExpr(c, n[2])
    if mode.kind != nkIntLit:
      localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
    else:
      m = mode.intVal
  let t = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
  closeScope(c)
  fixupTypeOf(c, prev, t)
  result = t.typ

proc semTypeIdent(c: PContext, n: PNode): PSym =
  if n.kind == nkSym:
    result = getGenSym(c, n.sym)
  else:
    result = pickSym(c, n, {skType, skGenericParam, skParam})
    if result.isNil:
      result = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared})
    if result != nil:
      markUsed(c, n.info, result)
      onUse(n.info, result)

      # alias syntax, see semSym for skTemplate, skMacro
      if result.kind in {skTemplate, skMacro} and sfNoalias notin result.flags:
        let t = semTypeExpr(c, n, nil)
        result = symFromType(c, t, n.info)

      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[0].sym
          if bound != nil: return bound
          return result
        if result.typ.sym == nil:
          localError(c.config, n.info, errTypeExpected)
          return errorSym(c, n)
        result = result.typ.sym.copySym(c.idgen)
        result.typ = exactReplica(result.typ)
        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.transitionGenericParamToType()
          result.typ.flags.excl tfWildcard
          return
        else:
          localError(c.config, n.info, errTypeExpected)
          return errorSym(c, n)
      if result.kind != skType and result.magic notin {mStatic, mType, mTypeOf}:
        # 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(c.config, n.info, errTypeExpected)
          return errorSym(c, n)
      if result.typ.kind != tyGenericParam:
        # XXX get rid of this hack!
        var oldInfo = n.info
        when defined(useNodeIds):
          let oldId = n.id
        reset(n[])
        when defined(useNodeIds):
          n.id = oldId
        n.transitionNoneToSym()
        n.sym = result
        n.info = oldInfo
        n.typ = result.typ
    else:
      localError(c.config, n.info, "identifier expected")
      result = errorSym(c, n)

proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
  result = nil
  inc c.inTypeContext

  if c.config.cmd == cmdIdeTools: suggestExpr(c, n)
  case n.kind
  of nkEmpty: result = n.typ
  of nkTypeOfExpr:
    # for ``typeof(countup(1,3))``, see ``tests/ttoseq``.
    checkSonsLen(n, 1, c.config)
    result = semTypeOf(c, n[0], prev)
    if result.kind == tyTypeDesc: result.flags.incl tfExplicit
  of nkPar:
    if n.len == 1: result = semTypeNode(c, n[0], prev)
    else:
      result = semAnonTuple(c, n, prev)
  of nkTupleConstr: result = semAnonTuple(c, n, prev)
  of nkCallKinds:
    let x = n[0]
    let ident = case x.kind
                of nkIdent: x.ident
                of nkSym: x.sym.name
                of nkClosedSymChoice, nkOpenSymChoice: x[0].sym.name
                else: nil
    if ident != nil and ident.s == "[]":
      let b = newNodeI(nkBracketExpr, n.info)
      for i in 1..<n.len: b.add(n[i])
      result = semTypeNode(c, b, prev)
    elif ident != nil and ident.id == ord(wDotDot):
      result = semRangeAux(c, n, prev)
    elif n[0].kind == nkNilLit and n.len == 2:
      result = semTypeNode(c, n[1], prev)
      if result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned}).kind in NilableTypes+GenericTypes:
        if tfNotNil in result.flags:
          result = freshType(c, result, prev)
          result.flags.excl(tfNotNil)
      else:
        localError(c.config, n.info, errGenerated, "invalid type")
    elif n[0].kind notin nkIdentKinds:
      result = semTypeExpr(c, n, prev)
    else:
      let op = considerQuotedIdent(c, n[0])
      if op.id == ord(wAnd) or op.id == ord(wOr) or op.s == "|":
        checkSonsLen(n, 3, c.config)
        var
          t1 = semTypeNode(c, n[1], nil)
          t2 = semTypeNode(c, n[2], nil)
        if t1 == nil:
          localError(c.config, n[1].info, errTypeExpected)
          result = newOrPrevType(tyError, prev, c)
        elif t2 == nil:
          localError(c.config, n[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[1], prev)
          if result.kind == tyTypeDesc and tfUnresolved notin result.flags:
            result = result.base
          if n[2].kind != nkNilLit:
            localError(c.config, n.info,
              "Invalid syntax. When used with a type, 'not' can be followed only by 'nil'")
          if notnil notin c.features and strictNotNil notin c.features:
            localError(c.config, n.info,
              "enable the 'not nil' annotation with {.experimental: \"notnil\".} or " &
              "  the `strict not nil` annotation with {.experimental: \"strictNotNil\".} " &
              "  the \"notnil\" one is going to be deprecated, so please use \"strictNotNil\"")
          let resolvedType = result.skipTypes({tyGenericInst, tyAlias, tySink, tyOwned})
          case resolvedType.kind
          of tyGenericParam, tyTypeDesc, tyFromExpr:
            # XXX: This is a really inappropraite hack, but it solves
            # https://github.com/nim-lang/Nim/issues/4907 for now.
            #
            # A proper solution is to introduce a new type kind such
            # as `tyNotNil[tyRef[SomeGenericParam]]`. This will allow
            # semtypinst to replace the generic param correctly in
            # situations like the following:
            #
            # type Foo[T] = object
            #   bar: ref T not nil
            #   baz: ref T
            #
            # The root of the problem is that `T` here must have a specific
            # ID that is bound to a concrete type during instantiation.
            # The use of `freshType` below breaks this. Another hack would
            # be to reuse the same ID for the not nil type, but this will
            # fail if the `T` parameter is referenced multiple times as in
            # the example above.
            #
            # I suggest revisiting this once the language decides on whether
            # `not nil` should be the default. We can then map nilable refs
            # to other types such as `Option[T]`.
            result = makeTypeFromExpr(c, newTree(nkStmtListType, n.copyTree))
          of NilableTypes + {tyGenericInvocation, tyForward}:
            result = freshType(c, result, prev)
            result.flags.incl(tfNotNil)
          else:
            localError(c.config, n.info, errGenerated, "invalid type")
        of 2:
          let negated = semTypeNode(c, n[1], prev)
          result = makeNotType(c, negated)
        else:
          localError(c.config, n.info, errGenerated, "invalid type")
      elif op.id == ord(wPtr):
        result = semAnyRef(c, n, tyPtr, prev)
      elif op.id == ord(wRef):
        result = semAnyRef(c, n, tyRef, prev)
      elif op.id == ord(wType):
        checkSonsLen(n, 2, c.config)
        result = semTypeOf(c, n[1], prev)
      elif op.s == "typeof" and n[0].kind == nkSym and n[0].sym.magic == mTypeOf:
        result = semTypeOf2(c, n, prev)
      elif op.s == "owned" and optOwnedRefs notin c.config.globalOptions and n.len == 2:
        result = semTypeExpr(c, n[1], prev)
      else:
        if c.inGenericContext > 0 and n.kind == nkCall:
          result = makeTypeFromExpr(c, n.copyTree)
        else:
          result = semTypeExpr(c, n, prev)
  of nkWhenStmt:
    var whenResult = semWhen(c, n, false)
    if whenResult.kind == nkStmtList: whenResult.transitionSonsKind(nkStmtListType)
    result = semTypeNode(c, whenResult, prev)
  of nkBracketExpr:
    checkMinSonsLen(n, 2, c.config)
    var head = n[0]
    var s = if head.kind notin nkCallKinds: semTypeIdent(c, head)
            else: symFromExpectedTypeNode(c, semExpr(c, head))
    case s.magic
    of mArray: result = semArray(c, n, prev)
    of mOpenArray: result = semContainer(c, n, tyOpenArray, "openarray", prev)
    of mUncheckedArray: result = semContainer(c, n, tyUncheckedArray, "UncheckedArray", prev)
    of mRange: result = semRange(c, n, prev)
    of mSet: result = semSet(c, n, prev)
    of mOrdinal: result = semOrdinal(c, n, prev)
    of mIterableType: result = semIterableType(c, n, prev)
    of mSeq:
      result = semContainer(c, n, tySequence, "seq", prev)
      if optSeqDestructors in c.config.globalOptions:
        incl result.flags, tfHasAsgn
    of mVarargs: result = semVarargs(c, n, prev)
    of mTypeDesc, mType, mTypeOf:
      result = makeTypeDesc(c, semTypeNode(c, n[1], nil))
      result.flags.incl tfExplicit
    of mStatic:
      result = semStaticType(c, n[1], prev)
    of mExpr:
      result = semTypeNode(c, n[0], nil)
      if result != nil:
        let old = result
        result = copyType(result, nextTypeId c.idgen, getCurrOwner(c))
        copyTypeProps(c.graph, c.idgen.module, result, old)
        for i in 1..<n.len:
          result.rawAddSon(semTypeNode(c, n[i], nil))
    of mDistinct:
      result = newOrPrevType(tyDistinct, prev, c)
      addSonSkipIntLit(result, semTypeNode(c, n[1], nil), c.idgen)
    of mVar:
      result = newOrPrevType(tyVar, prev, c)
      var base = semTypeNode(c, n[1], nil)
      if base.kind in {tyVar, tyLent}:
        localError(c.config, n.info, "type 'var var' is not allowed")
        base = base[0]
      addSonSkipIntLit(result, base, c.idgen)
    of mRef: result = semAnyRef(c, n, tyRef, prev)
    of mPtr: result = semAnyRef(c, n, tyPtr, prev)
    of mTuple: result = semTuple(c, n, prev)
    else: result = semGeneric(c, n, s, prev)
  of nkDotExpr:
    let typeExpr = semExpr(c, n)
    if typeExpr.typ.isNil:
      localError(c.config, n.info, "object constructor needs an object type;" &
          " for named arguments use '=' instead of ':'")
      result = errorType(c)
    elif typeExpr.typ.kind == tyFromExpr:
      result = typeExpr.typ
    elif typeExpr.typ.kind != tyTypeDesc:
      localError(c.config, n.info, errTypeExpected)
      result = errorType(c)
    else:
      result = typeExpr.typ.base
      if result.isMetaType and
         result.kind != tyUserTypeClass:
           # the dot expression may refer to a concept type in
           # a different module. allow a normal alias then.
        let preprocessed = semGenericStmt(c, n)
        result = makeTypeFromExpr(c, preprocessed.copyTree)
      else:
        let alias = maybeAliasType(c, result, prev)
        if alias != nil: result = alias
  of nkIdent, nkAccQuoted:
    var s = semTypeIdent(c, n)
    if s.typ == nil:
      if s.kind != skError: localError(c.config, n.info, errTypeExpected)
      result = newOrPrevType(tyError, prev, c)
    elif s.kind == skParam and s.typ.kind == tyTypeDesc:
      internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
      result = s.typ.base
    elif prev == nil:
      result = s.typ
    else:
      let alias = maybeAliasType(c, s.typ, prev)
      if alias != nil:
        result = alias
      elif prev.kind == tyGenericBody:
        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.itemId = s.typ.itemId
        result = prev
  of nkSym:
    let s = getGenSym(c, n.sym)
    if s.typ != nil and (s.kind == skType or s.typ.kind == tyTypeDesc):
      var t =
        if s.kind == skType:
          s.typ
        else:
          internalAssert c.config, s.typ.base.kind != tyNone and prev == nil
          s.typ.base
      let alias = maybeAliasType(c, t, prev)
      if alias != nil:
        result = alias
      elif prev == nil or prev.kind == tyGenericBody:
        result = t
      else:
        assignType(prev, t)
        result = prev
      markUsed(c, n.info, n.sym)
      onUse(n.info, n.sym)
    else:
      if s.kind != skError:
        if s.typ == nil:
          localError(c.config, n.info, "type expected, but symbol '$1' has no type." % [s.name.s])
        else:
          localError(c.config, n.info, "type expected, but got symbol '$1' of kind '$2'" %
            [s.name.s, s.kind.toHumanStr])
      result = newOrPrevType(tyError, prev, c)
  of nkObjectTy: result = semObjectNode(c, n, prev, {})
  of nkTupleTy: result = semTuple(c, n, prev)
  of nkTupleClassTy: result = newConstraint(c, tyTuple)
  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 = semVarOutType(c, n, prev, {})
  of nkOutTy: result = semVarOutType(c, n, prev, {tfIsOutParam})
  of nkDistinctTy: result = semDistinct(c, n, prev)
  of nkStaticTy: result = semStaticType(c, n[0], prev)
  of nkProcTy, nkIteratorTy:
    if n.len == 0 or n[0].kind == nkEmpty:
      # 0 length or empty param list with possible pragmas imply typeclass
      result = newTypeS(tyBuiltInTypeClass, c)
      let child = newTypeS(tyProc, c)
      if n.kind == nkIteratorTy:
        child.flags.incl tfIterator
      if n.len > 0 and n[1].kind != nkEmpty and n[1].len > 0:
        # typeclass with pragma
        let symKind = if n.kind == nkIteratorTy: skIterator else: skProc
        # dummy symbol for `pragma`:
        var s = newSymS(symKind, newIdentNode(getIdent(c.cache, "dummy"), n.info), c)
        s.typ = child
        # for now only call convention pragmas supported in proc typeclass
        pragma(c, s, n[1], {FirstCallConv..LastCallConv})
      result.addSonSkipIntLit(child, c.idgen)
    else:
      let symKind = if n.kind == nkIteratorTy: skIterator else: skProc
      result = semProcTypeWithScope(c, n, prev, symKind)
      if n.kind == nkIteratorTy and result.kind == tyProc:
        result.flags.incl(tfIterator)
  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)
  else:
    result = semTypeExpr(c, n, prev)
    when false:
      localError(c.config, n.info, "type expected, but got: " & renderTree(n))
      result = newOrPrevType(tyError, prev, c)
  n.typ = result
  dec c.inTypeContext

proc setMagicType(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  # source : https://en.wikipedia.org/wiki/Data_structure_alignment#x86
  m.typ.kind = kind
  m.typ.size = size
  # this usually works for most basic types
  # Assuming that since ARM, ARM64  don't support unaligned access
  # data is aligned to type size
  m.typ.align = size.int16

  # FIXME: proper support for clongdouble should be added.
  # long double size can be 8, 10, 12, 16 bytes depending on platform & compiler
  if kind in {tyFloat64, tyFloat, tyInt, tyUInt, tyInt64, tyUInt64} and size == 8:
    m.typ.align = int16(conf.floatInt64Align)

proc setMagicIntegral(conf: ConfigRef; m: PSym, kind: TTypeKind, size: int) =
  setMagicType(conf, m, kind, size)
  incl m.typ.flags, tfCheckedForDestructor

proc processMagicType(c: PContext, m: PSym) =
  case m.magic
  of mInt: setMagicIntegral(c.config, m, tyInt, c.config.target.intSize)
  of mInt8: setMagicIntegral(c.config, m, tyInt8, 1)
  of mInt16: setMagicIntegral(c.config, m, tyInt16, 2)
  of mInt32: setMagicIntegral(c.config, m, tyInt32, 4)
  of mInt64: setMagicIntegral(c.config, m, tyInt64, 8)
  of mUInt: setMagicIntegral(c.config, m, tyUInt, c.config.target.intSize)
  of mUInt8: setMagicIntegral(c.config, m, tyUInt8, 1)
  of mUInt16: setMagicIntegral(c.config, m, tyUInt16, 2)
  of mUInt32: setMagicIntegral(c.config, m, tyUInt32, 4)
  of mUInt64: setMagicIntegral(c.config, m, tyUInt64, 8)
  of mFloat: setMagicIntegral(c.config, m, tyFloat, c.config.target.floatSize)
  of mFloat32: setMagicIntegral(c.config, m, tyFloat32, 4)
  of mFloat64: setMagicIntegral(c.config, m, tyFloat64, 8)
  of mFloat128: setMagicIntegral(c.config, m, tyFloat128, 16)
  of mBool: setMagicIntegral(c.config, m, tyBool, 1)
  of mChar: setMagicIntegral(c.config, m, tyChar, 1)
  of mString:
    setMagicType(c.config, m, tyString, szUncomputedSize)
    rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
    if optSeqDestructors in c.config.globalOptions:
      incl m.typ.flags, tfHasAsgn
  of mCstring:
    setMagicIntegral(c.config, m, tyCstring, c.config.target.ptrSize)
    rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
  of mPointer: setMagicIntegral(c.config, m, tyPointer, c.config.target.ptrSize)
  of mNil: setMagicType(c.config, m, tyNil, c.config.target.ptrSize)
  of mExpr:
    if m.name.s == "auto":
      setMagicIntegral(c.config, m, tyAnything, 0)
    else:
      setMagicIntegral(c.config, m, tyUntyped, 0)
  of mStmt:
    setMagicIntegral(c.config, m, tyTyped, 0)
  of mTypeDesc, mType:
    setMagicIntegral(c.config, m, tyTypeDesc, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mStatic:
    setMagicType(c.config, m, tyStatic, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mVoidType:
    setMagicIntegral(c.config, m, tyVoid, 0)
  of mArray:
    setMagicType(c.config, m, tyArray, szUncomputedSize)
  of mOpenArray:
    setMagicType(c.config, m, tyOpenArray, szUncomputedSize)
  of mVarargs:
    setMagicType(c.config, m, tyVarargs, szUncomputedSize)
  of mRange:
    setMagicIntegral(c.config, m, tyRange, szUncomputedSize)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mSet:
    setMagicIntegral(c.config, m, tySet, szUncomputedSize)
  of mUncheckedArray:
    setMagicIntegral(c.config, m, tyUncheckedArray, szUncomputedSize)
  of mSeq:
    setMagicType(c.config, m, tySequence, szUncomputedSize)
    if optSeqDestructors in c.config.globalOptions:
      incl m.typ.flags, tfHasAsgn
    if defined(nimsuggest) or c.config.cmd == cmdCheck: # bug #18985
      discard
    else:
      assert c.graph.sysTypes[tySequence] == nil
    c.graph.sysTypes[tySequence] = m.typ
  of mOrdinal:
    setMagicIntegral(c.config, m, tyOrdinal, szUncomputedSize)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mIterableType:
    setMagicIntegral(c.config, m, tyIterable, 0)
    rawAddSon(m.typ, newTypeS(tyNone, c))
  of mPNimrodNode:
    incl m.typ.flags, tfTriggersCompileTime
    incl m.typ.flags, tfCheckedForDestructor
  of mException: discard
  of mBuiltinType:
    case m.name.s
    of "lent": setMagicType(c.config, m, tyLent, c.config.target.ptrSize)
    of "sink": setMagicType(c.config, m, tySink, szUncomputedSize)
    of "owned":
      setMagicType(c.config, m, tyOwned, c.config.target.ptrSize)
      incl m.typ.flags, tfHasOwned
    else: localError(c.config, m.info, errTypeExpected)
  else: localError(c.config, 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 =

  template addSym(result: PNode, s: PSym): untyped =
    if father != nil: addSonSkipIntLit(father, s.typ, c.idgen)
    if sfGenSym notin s.flags: addDecl(c, s)
    result.add newSymNode(s)

  result = copyNode(n)
  if n.kind != nkGenericParams:
    illFormedAst(n, c.config)
    return
  for i in 0..<n.len:
    var a = n[i]
    case a.kind
    of nkSym: result.addSym(a.sym)
    of nkIdentDefs:
      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[0].kind == tyNone:
              typ = newTypeWithSons(c, tyTypeDesc, @[newTypeS(tyNone, c)])
              incl typ.flags, tfCheckedForDestructor
          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:
          # the following line fixes ``TV2*[T:SomeNumber=TR] = array[0..1, T]``
          # from manyloc/named_argument_bug/triengine:
          def.typ = def.typ.skipTypes({tyTypeDesc})
          if not containsGenericType(def.typ):
            def = fitNode(c, typ, def, def.info)

      if typ == nil:
        typ = newTypeS(tyGenericParam, c)
        if father == nil: typ.flags.incl tfWildcard

      typ.flags.incl tfGenericTypeParam

      for j in 0..<a.len-2:
        var finalType: PType
        if j == 0:
          finalType = typ
        else:
          finalType = copyType(typ, nextTypeId c.idgen, typ.owner)
          copyTypeProps(c.graph, c.idgen.module, finalType, typ)
        # 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 paramName = a[j]
        var covarianceFlag = tfUnresolved

        if paramName.safeLen == 2:
          if not nimEnableCovariance or paramName[0].ident.s == "in":
            if father == nil or sfImportc notin father.sym.flags:
              localError(c.config, paramName.info, errInOutFlagNotExtern % $paramName[0])
          covarianceFlag = if paramName[0].ident.s == "in": tfContravariant
                          else: tfCovariant
          if father != nil: father.flags.incl tfCovariant
          paramName = paramName[1]

        var s = if finalType.kind == tyStatic or tfWildcard in typ.flags:
            newSymG(skGenericParam, paramName, c).linkTo(finalType)
          else:
            newSymG(skType, paramName, c).linkTo(finalType)

        if covarianceFlag != tfUnresolved: s.typ.flags.incl(covarianceFlag)
        if def.kind != nkEmpty: s.ast = def
        s.position = result.len
        result.addSym(s)
    else:
      illFormedAst(n, c.config)