modulegraphs.nim 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the module graph data structure. The module graph
  10. ## represents a complete Nim project. Single modules can either be kept in RAM
  11. ## or stored in a Sqlite database.
  12. ##
  13. ## The caching of modules is critical for 'nimsuggest' and is tricky to get
  14. ## right. If module E is being edited, we need autocompletion (and type
  15. ## checking) for E but we don't want to recompile depending
  16. ## modules right away for faster turnaround times. Instead we mark the module's
  17. ## dependencies as 'dirty'. Let D be a dependency of E. If D is dirty, we
  18. ## need to recompile it and all of its dependencies that are marked as 'dirty'.
  19. ## 'nimsuggest sug' actually is invoked for the file being edited so we know
  20. ## its content changed and there is no need to compute any checksums.
  21. ## Instead of a recursive algorithm, we use an iterative algorithm:
  22. ##
  23. ## - If a module gets recompiled, its dependencies need to be updated.
  24. ## - Its dependent module stays the same.
  25. ##
  26. import ast, intsets, tables, options, lineinfos, hashes, idents,
  27. incremental, btrees
  28. type
  29. ModuleGraph* = ref object
  30. modules*: seq[PSym] ## indexed by int32 fileIdx
  31. packageSyms*: TStrTable
  32. deps*: IntSet # the dependency graph or potentially its transitive closure.
  33. suggestMode*: bool # whether we are in nimsuggest mode or not.
  34. invalidTransitiveClosure: bool
  35. inclToMod*: Table[FileIndex, FileIndex] # mapping of include file to the
  36. # first module that included it
  37. importStack*: seq[FileIndex] # The current import stack. Used for detecting recursive
  38. # module dependencies.
  39. backend*: RootRef # minor hack so that a backend can extend this easily
  40. config*: ConfigRef
  41. cache*: IdentCache
  42. vm*: RootRef # unfortunately the 'vm' state is shared project-wise, this will
  43. # be clarified in later compiler implementations.
  44. doStopCompile*: proc(): bool {.closure.}
  45. usageSym*: PSym # for nimsuggest
  46. owners*: seq[PSym]
  47. methods*: seq[tuple[methods: TSymSeq, dispatcher: PSym]] # needs serialization!
  48. systemModule*: PSym
  49. sysTypes*: array[TTypeKind, PType]
  50. compilerprocs*: TStrTable
  51. exposed*: TStrTable
  52. intTypeCache*: array[-5..64, PType]
  53. opContains*, opNot*: PSym
  54. emptyNode*: PNode
  55. incr*: IncrementalCtx
  56. importModuleCallback*: proc (graph: ModuleGraph; m: PSym, fileIdx: FileIndex): PSym {.nimcall.}
  57. includeFileCallback*: proc (graph: ModuleGraph; m: PSym, fileIdx: FileIndex): PNode {.nimcall.}
  58. recordStmt*: proc (graph: ModuleGraph; m: PSym; n: PNode) {.nimcall.}
  59. cacheSeqs*: Table[string, PNode] # state that is shared to suppor the 'macrocache' API
  60. cacheCounters*: Table[string, BiggestInt]
  61. cacheTables*: Table[string, BTree[string, PNode]]
  62. proc hash*(x: FileIndex): Hash {.borrow.}
  63. proc stopCompile*(g: ModuleGraph): bool {.inline.} =
  64. result = g.doStopCompile != nil and g.doStopCompile()
  65. proc createMagic*(g: ModuleGraph; name: string, m: TMagic): PSym =
  66. result = newSym(skProc, getIdent(g.cache, name), nil, unknownLineInfo(), {})
  67. result.magic = m
  68. proc newModuleGraph*(cache: IdentCache; config: ConfigRef): ModuleGraph =
  69. result = ModuleGraph()
  70. initStrTable(result.packageSyms)
  71. result.deps = initIntSet()
  72. result.modules = @[]
  73. result.importStack = @[]
  74. result.inclToMod = initTable[FileIndex, FileIndex]()
  75. result.config = config
  76. result.cache = cache
  77. result.owners = @[]
  78. result.methods = @[]
  79. initStrTable(result.compilerprocs)
  80. initStrTable(result.exposed)
  81. result.opNot = createMagic(result, "not", mNot)
  82. result.opContains = createMagic(result, "contains", mInSet)
  83. result.emptyNode = newNode(nkEmpty)
  84. init(result.incr)
  85. result.recordStmt = proc (graph: ModuleGraph; m: PSym; n: PNode) {.nimcall.} =
  86. discard
  87. result.cacheSeqs = initTable[string, PNode]()
  88. result.cacheCounters = initTable[string, BiggestInt]()
  89. result.cacheTables = initTable[string, BTree[string, PNode]]()
  90. proc resetAllModules*(g: ModuleGraph) =
  91. initStrTable(g.packageSyms)
  92. g.deps = initIntSet()
  93. g.modules = @[]
  94. g.importStack = @[]
  95. g.inclToMod = initTable[FileIndex, FileIndex]()
  96. g.usageSym = nil
  97. g.owners = @[]
  98. g.methods = @[]
  99. initStrTable(g.compilerprocs)
  100. initStrTable(g.exposed)
  101. proc getModule*(g: ModuleGraph; fileIdx: FileIndex): PSym =
  102. if fileIdx.int32 >= 0 and fileIdx.int32 < g.modules.len:
  103. result = g.modules[fileIdx.int32]
  104. proc dependsOn(a, b: int): int {.inline.} = (a shl 15) + b
  105. proc addDep*(g: ModuleGraph; m: PSym, dep: FileIndex) =
  106. assert m.position == m.info.fileIndex.int32
  107. addModuleDep(g.incr, g.config, m.info.fileIndex, dep, isIncludeFile = false)
  108. if g.suggestMode:
  109. g.deps.incl m.position.dependsOn(dep.int)
  110. # we compute the transitive closure later when quering the graph lazily.
  111. # this improves efficiency quite a lot:
  112. #invalidTransitiveClosure = true
  113. proc addIncludeDep*(g: ModuleGraph; module, includeFile: FileIndex) =
  114. addModuleDep(g.incr, g.config, module, includeFile, isIncludeFile = true)
  115. discard hasKeyOrPut(g.inclToMod, includeFile, module)
  116. proc parentModule*(g: ModuleGraph; fileIdx: FileIndex): FileIndex =
  117. ## returns 'fileIdx' if the file belonging to this index is
  118. ## directly used as a module or else the module that first
  119. ## references this include file.
  120. if fileIdx.int32 >= 0 and fileIdx.int32 < g.modules.len and g.modules[fileIdx.int32] != nil:
  121. result = fileIdx
  122. else:
  123. result = g.inclToMod.getOrDefault(fileIdx)
  124. proc transitiveClosure(g: var IntSet; n: int) =
  125. # warshall's algorithm
  126. for k in 0..<n:
  127. for i in 0..<n:
  128. for j in 0..<n:
  129. if i != j and not g.contains(i.dependsOn(j)):
  130. if g.contains(i.dependsOn(k)) and g.contains(k.dependsOn(j)):
  131. g.incl i.dependsOn(j)
  132. proc markDirty*(g: ModuleGraph; fileIdx: FileIndex) =
  133. let m = g.getModule fileIdx
  134. if m != nil: incl m.flags, sfDirty
  135. proc markClientsDirty*(g: ModuleGraph; fileIdx: FileIndex) =
  136. # we need to mark its dependent modules D as dirty right away because after
  137. # nimsuggest is done with this module, the module's dirty flag will be
  138. # cleared but D still needs to be remembered as 'dirty'.
  139. if g.invalidTransitiveClosure:
  140. g.invalidTransitiveClosure = false
  141. transitiveClosure(g.deps, g.modules.len)
  142. # every module that *depends* on this file is also dirty:
  143. for i in 0i32..<g.modules.len.int32:
  144. let m = g.modules[i]
  145. if m != nil and g.deps.contains(i.dependsOn(fileIdx.int)):
  146. incl m.flags, sfDirty
  147. proc isDirty*(g: ModuleGraph; m: PSym): bool =
  148. result = g.suggestMode and sfDirty in m.flags