diff options
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/modulegraphs.nim | 18 | ||||
-rw-r--r-- | compiler/modules.nim | 1 |
2 files changed, 18 insertions, 1 deletions
diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 2c1671789..9a3caa663 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -25,7 +25,7 @@ ## - Its dependent module stays the same. ## -import ast, intsets +import ast, intsets, tables type ModuleGraph* = ref object @@ -34,6 +34,8 @@ type deps*: IntSet # the dependency graph or potentially its transitive closure. suggestMode*: bool # whether we are in nimsuggest mode or not. invalidTransitiveClosure: bool + inclToMod*: Table[int32, int32] # mapping of include file to the + # first module that included it {.this: g.} @@ -42,11 +44,13 @@ proc newModuleGraph*(): ModuleGraph = initStrTable(result.packageSyms) result.deps = initIntSet() result.modules = @[] + result.inclToMod = initTable[int32, int32]() proc resetAllModules*(g: ModuleGraph) = initStrTable(packageSyms) deps = initIntSet() modules = @[] + inclToMod = initTable[int32, int32]() proc getModule*(g: ModuleGraph; fileIdx: int32): PSym = if fileIdx >= 0 and fileIdx < modules.len: @@ -61,6 +65,18 @@ proc addDep*(g: ModuleGraph; m: PSym, dep: int32) = # this improve efficiency quite a lot: invalidTransitiveClosure = true +proc addIncludeDep*(g: ModuleGraph; module, includeFile: int32) = + discard hasKeyOrPut(inclToMod, includeFile, module) + +proc parentModule*(g: ModuleGraph; fileIdx: int32): int32 = + ## returns 'fileIdx' if the file belonging to this index is + ## directly used as a module or else the module that first + ## references this include file. + if fileIdx >= 0 and fileIdx < modules.len and modules[fileIdx] != nil: + result = fileIdx + else: + result = inclToMod.getOrDefault(fileIdx) + proc transitiveClosure(g: var IntSet; n: int) = # warshall's algorithm for k in 0..<n: diff --git a/compiler/modules.nim b/compiler/modules.nim index e901ba896..68df5f756 100644 --- a/compiler/modules.nim +++ b/compiler/modules.nim @@ -208,6 +208,7 @@ proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: int32; cache: IdentCache): PNode {.procvar.} = result = syntaxes.parseFile(fileIdx, cache) graph.addDep(s, fileIdx) + graph.addIncludeDep(s.position.int32, fileIdx) proc compileSystemModule*(graph: ModuleGraph; cache: IdentCache) = if magicsys.systemModule == nil: |