@@ -13,7 +13,7 @@ import std / [assertions, tables, sets]
1313from std / strutils import startsWith
1414import astdef, idents, msgs, options
1515import lineinfos as astli
16- import pathutils
16+ import pathutils # , modulegraphs
1717import " ../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos,
1818 nifindexes, nifreader]
1919import " ../dist/nimony/src/gear2" / modnames
@@ -258,6 +258,10 @@ proc writeLib(w: var Writer; dest: var TokenBuf; lib: PLib) =
258258proc writeSymDef (w: var Writer ; dest: var TokenBuf ; sym: PSym ) =
259259 dest.addParLe sdefTag, trLineInfo (w, sym.infoImpl)
260260 dest.addSymDef pool.syms.getOrIncl (w.toNifSymName (sym)), NoLineInfo
261+ if sfExported in sym.flagsImpl:
262+ dest.addIdent " x"
263+ else :
264+ dest.addDotToken
261265 if sym.magicImpl == mNone:
262266 dest.addDotToken
263267 else :
@@ -352,14 +356,14 @@ proc trInclude(w: var Writer; n: PNode) =
352356 w.deps.addParRi
353357
354358proc trImport (w: var Writer ; n: PNode ) =
355- w.deps.addParLe pool.tags.getOrIncl (toNifTag (n.kind)), trLineInfo (w, n.info)
356359 for child in n:
357- assert child.kind == nkSym
358- let s = child.sym
359- assert s.kindImpl == skModule
360- let fp = toFullPath (w.infos.config, s.positionImpl.FileIndex )
361- w.deps.addStrLit fp
362- w.deps.addParRi
360+ if child.kind == nkSym:
361+ w.deps.addParLe pool.tags.getOrIncl (toNifTag (n.kind)), trLineInfo (w, n.info)
362+ let s = child.sym
363+ assert s.kindImpl == skModule
364+ let fp = toFullPath (w.infos.config, s.positionImpl.FileIndex )
365+ w.deps.addStrLit fp
366+ w.deps.addParRi
363367
364368proc writeNode (w: var Writer ; dest: var TokenBuf ; n: PNode ) =
365369 if n == nil :
@@ -421,6 +425,7 @@ proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode) =
421425 var ast = n
422426 if n[namePos].kind == nkSym:
423427 ast = n[namePos].sym.astImpl
428+ if ast == nil : ast = n
424429 w.withNode dest, ast:
425430 # Process body and other parts
426431 for i in 0 ..< ast.len:
@@ -463,7 +468,8 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode) =
463468 inner.addParRi ()
464469
465470 let m = modname (w.moduleToNifSuffix, w.currentModule, w.infos.config)
466- let d = toGeneratedFile (config, AbsoluteFile (m), " .nif" ).string
471+ let nifFilename = AbsoluteFile (m).changeFileExt (" .nif" )
472+ let d = completeGeneratedFilePath (config, nifFilename).string
467473
468474 var dest = createTokenBuf (600 )
469475 dest.addParLe pool.tags.getOrIncl (toNifTag (nkStmtList)), rootInfo
@@ -472,7 +478,8 @@ proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode) =
472478 dest.add inner
473479 dest.addParRi ()
474480
475- writeFileAndIndex d, dest
481+ writeFile (dest, d)
482+ createIndex (d, false , dest[0 ].info)
476483
477484
478485# --------------------------- Loader (lazy!) -----------------------------------------------
@@ -536,48 +543,37 @@ type
536543
537544 DecodeContext * = object
538545 infos: LineInfoWriter
539- moduleIds: Table [string , int32 ]
546+ # moduleIds: Table[string, int32]
540547 types: Table [ItemId , (PType , NifIndexEntry )]
541548 syms: Table [ItemId , (PSym , NifIndexEntry )]
542549 mods: seq [NifModule ]
543550 cache: IdentCache
544- moduleToNifSuffix: Table [FileIndex , string ]
551+ # moduleToNifSuffix: Table[FileIndex, string]
545552
546553proc createDecodeContext * (config: ConfigRef ; cache: IdentCache ): DecodeContext =
547554 # # Supposed to be a global variable
548555 result = DecodeContext (infos: LineInfoWriter (config: config), cache: cache)
549556
550- proc idToIdx (x: int32 ): int {.inline .} =
551- assert x <= - 2 'i32
552- result = - (x+ 2 )
553-
554- proc cursorFromIndexEntry (c: var DecodeContext ; module: int32 ; entry: NifIndexEntry ;
557+ proc cursorFromIndexEntry (c: var DecodeContext ; module: FileIndex ; entry: NifIndexEntry ;
555558 buf: var TokenBuf ): Cursor =
556- let m = idToIdx (module)
557- let s = addr c.mods[m].stream
559+ let s = addr c.mods[module.int32 ].stream
558560 s.r.jumpTo entry.offset
559561 var buf = createTokenBuf (30 )
560562 nifcursors.parse (s[], buf, entry.info)
561563 result = cursorAt (buf, 0 )
562564
563- proc moduleId (c: var DecodeContext ; suffix: string ): int32 =
564- # We don't know the "real" FileIndex due to our mapping to a short "Module suffix"
565- # This is not a problem, we use negative `ItemId.module` values here and then
566- # there is no interference with in-memory-modules. Modulegraphs.nim already uses -1
567- # so we start at -2 here.
568- result = c.moduleIds.getOrDefault (suffix)
569- if result == 0 :
570- result = - int32 (c.moduleIds.len + 2 ) # negative index!
565+ proc moduleId (c: var DecodeContext ; suffix: string ): FileIndex =
566+ var isKnownFile = false
567+ result = c.infos.config.registerNifSuffix (suffix, isKnownFile)
568+ if not isKnownFile:
571569 let modFile = (getNimcacheDir (c.infos.config) / RelativeFile (suffix & " .nif" )).string
572570 let idxFile = (getNimcacheDir (c.infos.config) / RelativeFile (suffix & " .idx.nif" )).string
573- c.moduleIds[suffix] = result
574- c.mods.add NifModule (stream: nifstreams.open (modFile), index: readIndex (idxFile))
575- assert c.mods.len- 1 == idToIdx (result )
576-
577- proc getOffset (c: var DecodeContext ; module: int32 ; nifName: string ): NifIndexEntry =
578- assert module < 0 'i32
579- let index = idToIdx (module)
580- let ii = addr c.mods[index].index
571+ if result .int >= c.mods.len:
572+ c.mods.setLen (result .int + 1 )
573+ c.mods[result .int ] = NifModule (stream: nifstreams.open (modFile), index: readIndex (idxFile))
574+
575+ proc getOffset (c: var DecodeContext ; module: FileIndex ; nifName: string ): NifIndexEntry =
576+ let ii = addr c.mods[module.int32 ].index
581577 result = ii.public.getOrDefault (nifName)
582578 if result .offset == 0 :
583579 result = ii.private.getOrDefault (nifName)
@@ -601,10 +597,10 @@ proc loadTypeStub(c: var DecodeContext; t: SymId): PType =
601597 inc i
602598 if i < name.len and name[i] == '.' : inc i
603599 let suffix = name.substr (i)
604- let id = ItemId (module: moduleId (c, suffix), item: itemId)
600+ let id = ItemId (module: moduleId (c, suffix). int32 , item: itemId)
605601 result = c.types.getOrDefault (id)[0 ]
606602 if result == nil :
607- let offs = c.getOffset (id.module, name)
603+ let offs = c.getOffset (id.module. FileIndex , name)
608604 result = PType (itemId: id, uniqueId: id, kind: TTypeKind (k), state: Partial )
609605 c.types[id] = (result , offs)
610606
@@ -627,10 +623,10 @@ proc loadSymStub(c: var DecodeContext; t: SymId): PSym =
627623 let symAsStr = pool.syms[t]
628624 let sn = parseSymName (symAsStr)
629625 let module = moduleId (c, sn.module)
630- let val = addr c.mods[idToIdx ( module) ].symCounter
626+ let val = addr c.mods[module. int32 ].symCounter
631627 inc val[]
632628
633- let id = ItemId (module: module, item: val[])
629+ let id = ItemId (module: module. int32 , item: val[])
634630 result = c.syms.getOrDefault (id)[0 ]
635631 if result == nil :
636632 let offs = c.getOffset (module, symAsStr)
@@ -696,7 +692,7 @@ proc loadType*(c: var DecodeContext; t: PType) =
696692 if t.state != Partial : return
697693 t.state = Sealed
698694 var buf = createTokenBuf (30 )
699- var n = cursorFromIndexEntry (c, t.itemId.module, c.types[t.itemId][1 ], buf)
695+ var n = cursorFromIndexEntry (c, t.itemId.module. FileIndex , c.types[t.itemId][1 ], buf)
700696
701697 expect n, ParLe
702698 if n.tagId != tdefTag:
@@ -745,7 +741,7 @@ proc loadSym*(c: var DecodeContext; s: PSym) =
745741 if s.state != Partial : return
746742 s.state = Sealed
747743 var buf = createTokenBuf (30 )
748- var n = cursorFromIndexEntry (c, s.itemId.module, c.syms[s.itemId][1 ], buf)
744+ var n = cursorFromIndexEntry (c, s.itemId.module. FileIndex , c.syms[s.itemId][1 ], buf)
749745
750746 expect n, ParLe
751747 if n.tagId != sdefTag:
@@ -754,6 +750,17 @@ proc loadSym*(c: var DecodeContext; s: PSym) =
754750 expect n, SymbolDef
755751 # ignore the symbol's name, we have already used it to create this PSym instance!
756752 inc n
753+ if n.kind == Ident :
754+ if pool.strings[n.litId] == " x" :
755+ s.flagsImpl.incl sfExported
756+ inc n
757+ else :
758+ raiseAssert " expected `x` as the export marker"
759+ elif n.kind == DotToken :
760+ inc n
761+ else :
762+ raiseAssert " expected `x` or '.' but got " & $ n.kind
763+
757764 loadField s.magicImpl
758765 loadField s.flagsImpl
759766 loadField s.optionsImpl
@@ -894,20 +901,20 @@ proc loadNode(c: var DecodeContext; n: var Cursor): PNode =
894901 else :
895902 raiseAssert " Not yet implemented " & $ n.kind
896903
897-
898- proc loadNifModule * (c: var DecodeContext ; f: FileIndex ): PNode =
899- let moduleSuffix = modname (c.moduleToNifSuffix, f. int , c. infos.config)
900- let modFile = toGeneratedFile (c.infos.config, AbsoluteFile (moduleSuffix), " .nif" ).string
901-
902- var buf = createTokenBuf (300 )
903- var s = nifstreams.open (modFile)
904- # XXX We can optimize this here and only load the top level entries!
905- try :
906- nifcursors.parse (s, buf, NoLineInfo )
907- finally :
908- nifstreams.close (s)
909- var n = cursorAt (buf, 0 )
910- result = loadNode (c, n)
904+ when false :
905+ proc loadNifModule * (c: var DecodeContext ; f: FileIndex ): PNode =
906+ let moduleSuffix = moduleSuffix (c.infos.config, f )
907+ let modFile = toGeneratedFile (c.infos.config, AbsoluteFile (moduleSuffix), " .nif" ).string
908+
909+ var buf = createTokenBuf (300 )
910+ var s = nifstreams.open (modFile)
911+ # XXX We can optimize this here and only load the top level entries!
912+ try :
913+ nifcursors.parse (s, buf, NoLineInfo )
914+ finally :
915+ nifstreams.close (s)
916+ var n = cursorAt (buf, 0 )
917+ result = loadNode (c, n)
911918
912919when isMainModule :
913920 import std / syncio
0 commit comments