Skip to content

Commit 034ced8

Browse files
committed
partially supports type sections
1 parent d55634f commit 034ced8

File tree

6 files changed

+572
-49
lines changed

6 files changed

+572
-49
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,33 @@ type
88
DecodeContext = object
99
graph: ModuleGraph
1010
nifSymToPSym: Table[string, PSym] # foo.1.modsuffix -> PSym
11+
types: Table[SymId, PType]
12+
owner: PSym
13+
sysTypes: Table[TTypeKind, PSym]
14+
idgen: IdGenerator
1115

1216
proc nodeKind(n: Cursor): TNodeKind {.inline.} =
1317
assert n.kind == ParLe
1418
pool.tags[n.tagId].parseNodeKind()
1519

16-
var sysTypes: Table[TTypeKind, PType]
20+
const SysTypeKinds = {tyBool, tyChar, tyString, tyInt .. tyUInt64}
1721

18-
proc getSysType(typeKind: TTypeKind): PType =
19-
# This will be replaced with magicsys.getSysType
20-
assert typeKind in {tyBool, tyChar, tyInt .. tyUInt64}
21-
if typeKind in sysTypes:
22-
result = sysTypes[typeKind]
22+
proc getSysTypeSym(c: var DecodeContext; typeKind: TTypeKind): PSym =
23+
assert typeKind in SysTypeKinds
24+
if typeKind in c.sysTypes:
25+
result = c.sysTypes[typeKind]
2326
else:
24-
result = PType(itemId: ItemId(module: 0, item: typeKind.int32), kind: typeKind)
25-
sysTypes[typeKind] = result
27+
let ident = c.graph.cache.getIdent(toNifTag typeKind)
28+
result = newSym(skType, ident, c.idgen, c.owner, unknownLineInfo)
29+
var typ = newType(typeKind, c.idgen, nil)
30+
typ.sym = result
31+
result.typ = typ
32+
c.sysTypes[typeKind] = result
33+
34+
proc getSysType(c: var DecodeContext; typeKind: TTypeKind): PType =
35+
# This will be replaced with magicsys.getSysType
36+
assert typeKind in SysTypeKinds
37+
getSysTypeSym(c, typeKind).typ
2638

2739
type
2840
SplittedNifSym = object
@@ -48,6 +60,9 @@ proc splitNifSym(s: string): SplittedNifSym =
4860
dec i
4961

5062
proc fromNif(c: var DecodeContext; n: var Cursor): PNode
63+
proc fromNifType(c: var DecodeContext; n: var Cursor): PType
64+
65+
include nifdecodertypes
5166

5267
proc fromNifSymbol(c: var DecodeContext; n: var Cursor): PSym =
5368
result = c.nifSymToPSym[pool.syms[n.symId]]
@@ -56,6 +71,7 @@ proc fromNifSymbol(c: var DecodeContext; n: var Cursor): PSym =
5671
proc fromNifSymDef(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
5772
assert n.nodeKind == nkSym
5873
let symKind = case kind:
74+
of nkTypeSection: skType
5975
of nkVarSection: skVar
6076
of nkLetSection: skLet
6177
of nkImportStmt: skModule
@@ -109,13 +125,48 @@ proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
109125
result[0] = newNodeI(nkIdentDefs, unknownLineInfo, 3)
110126
inc n
111127
result[0][0] = fromNifSymDef(c, n, kind)
112-
result[0][1] = fromNif(c, n)
128+
if n.kind == DotToken:
129+
result[0][1] = newNode(nkEmpty)
130+
inc n
131+
else:
132+
let typeSym = fromNifType(c, n).sym
133+
assert typeSym != nil
134+
result[0][1] = typeSym.newSymNode
135+
result[0][0].sym.typ = typeSym.typ
113136
result[0][2] = fromNif(c, n)
114137
assert n.kind == ParRi # nkIdentDefs
115138
inc n
116139
assert n.kind == ParRi
117140
inc n
118141

142+
proc fromNifTypeSection(c: var DecodeContext; n: var Cursor): PNode =
143+
result = newNodeI(nkTypeDef, unknownLineInfo, 3)
144+
inc n
145+
let sym = fromNifSymDef(c, n, nkTypeSection)
146+
if n.kind == DotToken:
147+
result[0] = sym
148+
else:
149+
var postfix = newNodeI(nkPostfix, unknownLineInfo, 2)
150+
postfix.add newIdentNode(c.graph.cache.getIdent("*"), unknownLineInfo)
151+
postfix.add sym
152+
result[0] = postfix
153+
inc n
154+
155+
# TODO: pragma
156+
#result.add fromNif(c, n)
157+
assert n.kind == DotToken
158+
inc n
159+
160+
# TODO: generics
161+
result[1] = fromNif(c, n)
162+
163+
# type body
164+
result[2] = fromNifType(c, n).sym.newSymNode
165+
sym.sym.typ = result[2].sym.typ
166+
167+
assert n.kind == ParRi
168+
inc n
169+
119170
proc fromNifImport(c: var DecodeContext; n: var Cursor): PNode =
120171
result = newNode(nkImportStmt)
121172
inc n
@@ -157,7 +208,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
157208
else:
158209
assert false, "Unknown int literal suffix " & suffix
159210
tyNone
160-
result = newIntTypeNode(v, getSysType(kind))
211+
result = newIntTypeNode(v, c.getSysType(kind))
161212
of UIntLit:
162213
let v = pool.uintegers[n.uintId]
163214
inc n
@@ -175,7 +226,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
175226
else:
176227
assert false, "Unknown uint literal suffix " & suffix
177228
tyNone
178-
result = newIntTypeNode(cast[BiggestInt](v), getSysType(kind))
229+
result = newIntTypeNode(cast[BiggestInt](v), c.getSysType(kind))
179230
of FloatLit:
180231
let v = pool.floats[n.floatId]
181232
inc n
@@ -192,7 +243,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
192243
assert false, "Unknown uint literal suffix " & suffix
193244
(nkNone, tyNone)
194245
result = newFloatNode(kind[0], v)
195-
result.typ() = getSysType(kind[1])
246+
result.typ() = c.getSysType(kind[1])
196247
else:
197248
assert false, "invalid node in suf node " & $n.kind
198249
inc n
@@ -214,26 +265,28 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
214265
result = newIntNode(nkCharLit, n.charLit.int)
215266
inc n
216267
of IntLit:
217-
result = newIntTypeNode(pool.integers[n.intId], getSysType(tyInt))
268+
result = newIntTypeNode(pool.integers[n.intId], c.getSysType(tyInt))
218269
inc n
219270
of UIntLit:
220-
result = newIntTypeNode(cast[BiggestInt](pool.uintegers[n.uintId]), getSysType(tyUInt))
271+
result = newIntTypeNode(cast[BiggestInt](pool.uintegers[n.uintId]), c.getSysType(tyUInt))
221272
inc n
222273
of FloatLit:
223274
result = newFloatNode(nkFloatLit, pool.floats[n.floatId])
224-
result.typ() = getSysType(tyFloat)
275+
result.typ() = c.getSysType(tyFloat)
225276
inc n
226277
of ParLe:
227278
let kind = n.nodeKind
228279
case kind:
229-
of nkStmtList:
230-
result = newNode(nkStmtList)
280+
of nkPostfix, nkTypeSection, nkStmtList:
281+
result = newNode(kind)
231282
inc n
232283
while n.kind != ParRi:
233284
result.add fromNif(c, n)
234285
inc n
235286
of nkVarSection, nkLetSection:
236287
result = fromNifLocal(c, n, kind)
288+
of nkTypeDef:
289+
result = fromNifTypeSection(c, n)
237290
of nkImportStmt:
238291
result = fromNifImport(c, n)
239292
of nkNone:
@@ -259,6 +312,8 @@ proc loadNif(stream: var Stream; modulePath: AbsoluteFile; graph: ModuleGraph):
259312
let modSuffix = moduleSuffix(modulePath.string, cast[seq[string]](graph.config.searchPaths))
260313
let nifModSym = modSym.name.s & '.' & $modSym.disamb & '.' & modSuffix
261314
c.nifSymToPSym[nifModSym] = modSym
315+
c.owner = modSym
316+
c.idgen = idGeneratorFromModule(modSym)
262317

263318
result = fromNif(c, n)
264319

compiler/icnif/nifdecodertypes.nim

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# included from nifdecoder.nim
2+
3+
proc expect(n: var Cursor; k: NifKind) =
4+
if n.kind == k:
5+
inc n
6+
else:
7+
when defined(debug):
8+
writeStackTrace()
9+
quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n
10+
11+
proc readTypeKind(n: var Cursor; tag: string): TTypeKind =
12+
if tag.len == 1:
13+
case tag[0]
14+
of 'i':
15+
inc n
16+
assert n.kind == IntLit
17+
case pool.integers[n.intId]
18+
of -1: result = tyInt
19+
of 8: result = tyInt8
20+
of 16: result = tyInt16
21+
of 32: result = tyInt32
22+
of 64: result = tyInt64
23+
else: assert false
24+
inc n
25+
of 'u':
26+
inc n
27+
assert n.kind == IntLit
28+
case pool.integers[n.intId]
29+
of -1: result = tyUInt
30+
of 8: result = tyUInt8
31+
of 16: result = tyUInt16
32+
of 32: result = tyUInt32
33+
of 64: result = tyUInt64
34+
else: assert false
35+
inc n
36+
of 'f':
37+
inc n
38+
assert n.kind == IntLit
39+
case pool.integers[n.intId]
40+
of -1: result = tyFloat
41+
of 32: result = tyFloat32
42+
of 64: result = tyFloat64
43+
else: assert false
44+
inc n
45+
of 'c':
46+
inc n
47+
assert n.kind == IntLit
48+
case pool.integers[n.intId]
49+
of 8: result = tyChar
50+
else: assert false
51+
inc n
52+
else:
53+
result = parseTypeKind(tag)
54+
inc n
55+
else:
56+
result = parseTypeKind(tag)
57+
inc n
58+
59+
proc fromNifTypeImpl(c: var DecodeContext; n: var Cursor; kind: TTypeKind; res: PType) =
60+
case kind
61+
of tyFromExpr, tyEnum:
62+
res.n = fromNif(c, n)
63+
of tyStatic:
64+
res.addAllowNil fromNifType(c, n)
65+
res.n = fromNif(c, n)
66+
of tyObject:
67+
# inheritance:
68+
res.addAllowNil fromNifType(c, n)
69+
res.n = fromNif(c, n)
70+
else:
71+
while n.kind != ParRi:
72+
res.addAllowNil fromNifType(c, n)
73+
expect n, ParRi
74+
75+
proc fromNifType(c: var DecodeContext; n: var Cursor): PType =
76+
case n.kind
77+
of Symbol:
78+
let s = n.symId
79+
result = c.types.getOrDefault(s)
80+
when true:
81+
assert result != nil
82+
else:
83+
if result == nil:
84+
let symA = c.syms.getOrDefault(s).sym
85+
if symA != nil:
86+
assert symA.kind == skType
87+
result = symA.typ
88+
else:
89+
result = loadType(s, c)
90+
c.types[s] = LoadedType(state: Loaded, typ: result)
91+
of ParLe:
92+
let tag = pool.tags[n.tag]
93+
if tag == "missing":
94+
result = nil
95+
else:
96+
let k = readTypeKind(n, tag)
97+
if k in SysTypeKinds:
98+
result = getSysType(c, k)
99+
else:
100+
#echo "Create non SysTypeKinds type: ", k, ": ", tag
101+
result = newType(k, c.idgen, c.owner)
102+
if n.kind == ParLe and pool.tags[n.tag] == "tf":
103+
inc n
104+
if n.kind == Ident:
105+
result.flags = parseTypeFlags pool.strings[n.litId]
106+
inc n
107+
else:
108+
expect n, Ident
109+
expect n, ParRi
110+
fromNifTypeImpl c, n, k, result
111+
else:
112+
expect n, ParLe

0 commit comments

Comments
 (0)