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
1216proc 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
2739type
2840 SplittedNifSym = object
@@ -48,6 +60,9 @@ proc splitNifSym(s: string): SplittedNifSym =
4860 dec i
4961
5062proc fromNif (c: var DecodeContext ; n: var Cursor ): PNode
63+ proc fromNifType (c: var DecodeContext ; n: var Cursor ): PType
64+
65+ include nifdecodertypes
5166
5267proc 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 =
5671proc 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+
119170proc 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
0 commit comments