Skip to content

Commit 2910048

Browse files
committed
support ic
1 parent 0076836 commit 2910048

File tree

7 files changed

+93
-23
lines changed

7 files changed

+93
-23
lines changed

compiler/ic/ic.nim

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type
3636
bodies*: PackedTree # other trees. Referenced from typ.n and sym.ast by their position.
3737
#producedGenerics*: Table[GenericKey, SymId]
3838
exports*: seq[(LitId, int32)]
39+
hidden*: seq[(LitId, int32)]
3940
reexports*: seq[(LitId, PackedItemId)]
4041
compilerProcs*: seq[(LitId, int32)]
4142
converters*, methods*, trmacros*, pureEnums*: seq[int32]
@@ -177,6 +178,10 @@ proc addIncludeFileDep*(c: var PackedEncoder; m: var PackedModule; f: FileIndex)
177178
proc addImportFileDep*(c: var PackedEncoder; m: var PackedModule; f: FileIndex) =
178179
m.imports.add toLitId(f, c, m)
179180

181+
proc addHidden*(c: var PackedEncoder; m: var PackedModule; s: PSym) =
182+
let nameId = getOrIncl(m.sh.strings, s.name.s)
183+
m.hidden.add((nameId, s.itemId.item))
184+
180185
proc addExported*(c: var PackedEncoder; m: var PackedModule; s: PSym) =
181186
let nameId = getOrIncl(m.sh.strings, s.name.s)
182187
m.exports.add((nameId, s.itemId.item))
@@ -525,7 +530,7 @@ proc loadRodFile*(filename: AbsoluteFile; m: var PackedModule; config: ConfigRef
525530
loadTabSection floatsSection, m.sh.floats
526531

527532
loadSeqSection exportsSection, m.exports
528-
533+
loadSeqSection hiddenSection, m.hidden
529534
loadSeqSection reexportsSection, m.reexports
530535

531536
loadSeqSection compilerProcsSection, m.compilerProcs
@@ -591,7 +596,7 @@ proc saveRodFile*(filename: AbsoluteFile; encoder: var PackedEncoder; m: var Pac
591596
storeTabSection floatsSection, m.sh.floats
592597

593598
storeSeqSection exportsSection, m.exports
594-
599+
storeSeqSection hiddenSection, m.hidden
595600
storeSeqSection reexportsSection, m.reexports
596601

597602
storeSeqSection compilerProcsSection, m.compilerProcs
@@ -657,7 +662,9 @@ type
657662
syms: seq[PSym] # indexed by itemId
658663
types: seq[PType]
659664
module*: PSym # the one true module symbol.
660-
iface: Table[PIdent, seq[PackedItemId]] # PackedItemId so that it works with reexported symbols too
665+
iface, ifaceHidden: Table[PIdent, seq[PackedItemId]]
666+
# PackedItemId so that it works with reexported symbols too
667+
# ifaceHidden includes private symbols
661668

662669
PackedModuleGraph* = seq[LoadedModule] # indexed by FileIndex
663670

@@ -884,12 +891,18 @@ proc newPackage(config: ConfigRef; cache: IdentCache; fileIdx: FileIndex): PSym
884891
proc setupLookupTables(g: var PackedModuleGraph; conf: ConfigRef; cache: IdentCache;
885892
fileIdx: FileIndex; m: var LoadedModule) =
886893
m.iface = initTable[PIdent, seq[PackedItemId]]()
894+
m.ifaceHidden = initTable[PIdent, seq[PackedItemId]]()
887895
for e in m.fromDisk.exports:
888896
let nameLit = e[0]
889897
m.iface.mgetOrPut(cache.getIdent(m.fromDisk.sh.strings[nameLit]), @[]).add(PackedItemId(module: LitId(0), item: e[1]))
890898
for re in m.fromDisk.reexports:
891899
let nameLit = re[0]
892900
m.iface.mgetOrPut(cache.getIdent(m.fromDisk.sh.strings[nameLit]), @[]).add(re[1])
901+
m.ifaceHidden.mgetOrPut(cache.getIdent(m.fromDisk.sh.strings[nameLit]), @[]).add(re[1])
902+
903+
for e in m.fromDisk.hidden:
904+
let nameLit = e[0]
905+
m.ifaceHidden.mgetOrPut(cache.getIdent(m.fromDisk.sh.strings[nameLit]), @[]).add(PackedItemId(module: LitId(0), item: e[1]))
893906

894907
let filename = AbsoluteFile toFullPath(conf, fileIdx)
895908
# We cannot call ``newSym`` here, because we have to circumvent the ID
@@ -1055,24 +1068,29 @@ type
10551068
values: seq[PackedItemId]
10561069
i, module: int
10571070

1071+
template interfSelect(a: LoadedModule, importHidden: bool): auto =
1072+
var ret = a.iface.addr
1073+
if importHidden: ret = a.ifaceHidden.addr
1074+
ret[]
1075+
10581076
proc initRodIter*(it: var RodIter; config: ConfigRef, cache: IdentCache;
10591077
g: var PackedModuleGraph; module: FileIndex;
1060-
name: PIdent): PSym =
1078+
name: PIdent, importHidden: bool): PSym =
10611079
it.decoder = PackedDecoder(
10621080
lastModule: int32(-1),
10631081
lastLit: LitId(0),
10641082
lastFile: FileIndex(-1),
10651083
config: config,
10661084
cache: cache)
1067-
it.values = g[int module].iface.getOrDefault(name)
1085+
it.values = g[int module].interfSelect(importHidden).getOrDefault(name)
10681086
it.i = 0
10691087
it.module = int(module)
10701088
if it.i < it.values.len:
10711089
result = loadSym(it.decoder, g, int(module), it.values[it.i])
10721090
inc it.i
10731091

10741092
proc initRodIterAllSyms*(it: var RodIter; config: ConfigRef, cache: IdentCache;
1075-
g: var PackedModuleGraph; module: FileIndex): PSym =
1093+
g: var PackedModuleGraph; module: FileIndex, importHidden: bool): PSym =
10761094
it.decoder = PackedDecoder(
10771095
lastModule: int32(-1),
10781096
lastLit: LitId(0),
@@ -1081,7 +1099,7 @@ proc initRodIterAllSyms*(it: var RodIter; config: ConfigRef, cache: IdentCache;
10811099
cache: cache)
10821100
it.values = @[]
10831101
it.module = int(module)
1084-
for v in g[int module].iface.values:
1102+
for v in g[int module].interfSelect(importHidden).values:
10851103
it.values.add v
10861104
it.i = 0
10871105
if it.i < it.values.len:
@@ -1095,19 +1113,19 @@ proc nextRodIter*(it: var RodIter; g: var PackedModuleGraph): PSym =
10951113

10961114
iterator interfaceSymbols*(config: ConfigRef, cache: IdentCache;
10971115
g: var PackedModuleGraph; module: FileIndex;
1098-
name: PIdent): PSym =
1116+
name: PIdent, importHidden: bool): PSym =
10991117
setupDecoder()
1100-
let values = g[int module].iface.getOrDefault(name)
1118+
let values = g[int module].interfSelect(importHidden).getOrDefault(name)
11011119
for pid in values:
11021120
let s = loadSym(decoder, g, int(module), pid)
11031121
assert s != nil
11041122
yield s
11051123

11061124
proc interfaceSymbol*(config: ConfigRef, cache: IdentCache;
11071125
g: var PackedModuleGraph; module: FileIndex;
1108-
name: PIdent): PSym =
1126+
name: PIdent, importHidden: bool): PSym =
11091127
setupDecoder()
1110-
let values = g[int module].iface.getOrDefault(name)
1128+
let values = g[int module].interfSelect(importHidden).getOrDefault(name)
11111129
result = loadSym(decoder, g, int(module), values[0])
11121130

11131131
proc idgenFromLoadedModule*(m: LoadedModule): IdGenerator =
@@ -1128,7 +1146,7 @@ proc rodViewer*(rodfile: AbsoluteFile; config: ConfigRef, cache: IdentCache) =
11281146
let err = loadRodFile(rodfile, m, config, ignoreConfig=true)
11291147
if err != ok:
11301148
echo "Error: could not load: ", rodfile.string, " reason: ", err
1131-
quit 1
1149+
doAssert false # quit would prevent getting a stacktrace
11321150

11331151
when true:
11341152
echo "exports:"
@@ -1142,6 +1160,10 @@ proc rodViewer*(rodfile: AbsoluteFile; config: ConfigRef, cache: IdentCache) =
11421160
echo " ", m.sh.strings[ex[0]]
11431161
# reexports*: seq[(LitId, PackedItemId)]
11441162

1163+
echo "hidden: " & $m.hidden.len
1164+
for ex in m.hidden:
1165+
echo " ", m.sh.strings[ex[0]], " local ID: ", ex[1]
1166+
11451167
echo "all symbols"
11461168
for i in 0..high(m.sh.syms):
11471169
if m.sh.syms[i].name != LitId(0):

compiler/ic/rodfiles.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type
1919
integersSection
2020
floatsSection
2121
exportsSection
22+
hiddenSection
2223
reexportsSection
2324
compilerProcsSection
2425
trmacrosSection

compiler/modulegraphs.nim

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type
2929
patterns*: seq[LazySym]
3030
pureEnums*: seq[LazySym]
3131
interf: TStrTable
32-
interfAll: TStrTable
32+
interfHidden: TStrTable
3333
uniqueName*: Rope
3434

3535
Operators* = object
@@ -162,16 +162,15 @@ proc toBase64a(s: cstring, len: int): string =
162162
result.add cb64[(a and 3) shl 4]
163163

164164
template interfSelect(iface: Iface, importHidden: bool): TStrTable =
165-
var ret: ptr TStrTable # without intermediate ptr, it creates a copy and compiler becomes 15x slower!
166-
if importHidden: ret = iface.interfAll.addr
167-
else: ret = iface.interf.addr
165+
var ret = iface.interf.addr # without intermediate ptr, it creates a copy and compiler becomes 15x slower!
166+
if importHidden: ret = iface.interfHidden.addr
168167
ret[]
169168

170169
template semtab(g: ModuleGraph, m: PSym): TStrTable =
171170
g.ifaces[m.position].interf
172171

173172
template semtabAll*(g: ModuleGraph, m: PSym): TStrTable =
174-
g.ifaces[m.position].interfAll
173+
g.ifaces[m.position].interfHidden
175174

176175
proc initStrTables*(g: ModuleGraph, m: PSym) =
177176
initStrTable(semtab(g, m))
@@ -213,7 +212,7 @@ proc initModuleIter*(mi: var ModuleIter; g: ModuleGraph; m: PSym; name: PIdent):
213212
mi.fromRod = isCachedModule(g, mi.modIndex)
214213
mi.importHidden = optImportHidden in m.options
215214
if mi.fromRod:
216-
result = initRodIter(mi.rodIt, g.config, g.cache, g.packed, FileIndex mi.modIndex, name)
215+
result = initRodIter(mi.rodIt, g.config, g.cache, g.packed, FileIndex mi.modIndex, name, mi.importHidden)
217216
else:
218217
result = initIdentIter(mi.ti, g.ifaces[mi.modIndex].interfSelect(mi.importHidden), name)
219218

@@ -224,22 +223,24 @@ proc nextModuleIter*(mi: var ModuleIter; g: ModuleGraph): PSym =
224223
result = nextIdentIter(mi.ti, g.ifaces[mi.modIndex].interfSelect(mi.importHidden))
225224

226225
iterator allSyms*(g: ModuleGraph; m: PSym): PSym =
226+
let importHidden = optImportHidden in m.options
227227
if isCachedModule(g, m):
228228
var rodIt: RodIter
229-
var r = initRodIterAllSyms(rodIt, g.config, g.cache, g.packed, FileIndex m.position)
229+
var r = initRodIterAllSyms(rodIt, g.config, g.cache, g.packed, FileIndex m.position, importHidden)
230230
while r != nil:
231231
yield r
232232
r = nextRodIter(rodIt, g.packed)
233233
else:
234-
for s in g.ifaces[m.position].interfSelect(optImportHidden in m.options).data:
234+
for s in g.ifaces[m.position].interfSelect(importHidden).data:
235235
if s != nil:
236236
yield s
237237

238238
proc someSym*(g: ModuleGraph; m: PSym; name: PIdent): PSym =
239+
let importHidden = optImportHidden in m.options
239240
if isCachedModule(g, m):
240-
result = interfaceSymbol(g.config, g.cache, g.packed, FileIndex(m.position), name)
241+
result = interfaceSymbol(g.config, g.cache, g.packed, FileIndex(m.position), name, importHidden)
241242
else:
242-
result = strTableGet(g.ifaces[m.position].interfSelect(optImportHidden in m.options), name)
243+
result = strTableGet(g.ifaces[m.position].interfSelect(importHidden), name)
243244

244245
proc systemModuleSym*(g: ModuleGraph; name: PIdent): PSym =
245246
result = someSym(g, g.systemModule, name)
@@ -376,7 +377,10 @@ template onDefAux(info: TLineInfo; s0: PSym, c0: untyped, isFwd: bool) =
376377
# unfortunately, can't use `c.isTopLevel` because the scope isn't closed yet
377378
top = c.currentScope.depthLevel <= 3
378379
else: top = c.currentScope.depthLevel <= 2
379-
if top and c.module != nil: strTableAdd(semtabAll(c.graph, c.module), s0)
380+
if top and c.module != nil:
381+
strTableAdd(semtabAll(c.graph, c.module), s0)
382+
if c.config.symbolFiles != disabledSf:
383+
addHidden(c.encoder, c.packedRepr, s0)
380384

381385
when defined(nimfind):
382386
template onUse*(info: TLineInfo; s: PSym) =

testament/categories.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ proc icTests(r: var TResults; testsDir: string, cat: Category, options: string)
500500

501501
const tempExt = "_temp.nim"
502502
for it in walkDirRec(testsDir / "ic"):
503+
# for it in ["tests/ic/timports.nim"]: # debugging: to try a specific test
503504
if isTestFile(it) and not it.endsWith(tempExt):
504505
let nimcache = nimcacheDir(it, options, getTestSpecTarget())
505506
removeDir(nimcache)

tests/ic/mimports.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from mimportsb {.all.} import fnb1, hfnb3
2+
3+
proc fn1*(): int = 1
4+
proc fn2*(): int = 2
5+
proc hfn3(): int = 3
6+
proc hfn4(): int = 4
7+
proc hfn5(): int = 5
8+
9+
export mimportsb.fnb2, hfnb3

tests/ic/mimportsb.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
proc fnb1*(): int = 1
2+
proc fnb2*(): int = 2
3+
proc hfnb3(): int = 3
4+
proc hfnb4(): int = 4

tests/ic/timports.nim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import mimports
2+
doAssert fn1() == 1
3+
doAssert not declared(hfn3)
4+
5+
#!EDIT!#
6+
7+
import mimports {.all.}
8+
doAssert fn1() == 1
9+
doAssert declared(hfn3)
10+
doAssert hfn3() == 3
11+
doAssert mimports.hfn4() == 4
12+
13+
# reexports
14+
doAssert not declared(fnb1)
15+
doAssert not declared(hfnb4)
16+
doAssert fnb2() == 2
17+
doAssert hfnb3() == 3
18+
19+
#!EDIT!#
20+
21+
from mimports {.all.} import hfn3
22+
doAssert not declared(fn1)
23+
from mimports {.all.} as bar import fn1
24+
doAssert fn1() == 1
25+
doAssert hfn3() == 3
26+
doAssert not declared(hfn4)
27+
doAssert declared(mimports.hfn4)
28+
doAssert mimports.hfn4() == 4
29+
doAssert bar.hfn4() == 4

0 commit comments

Comments
 (0)