| Safe Haskell | Ignore |
|---|---|
| Language | GHC2021 |
GHC.Unit.Module.Graph
Description
A module graph should be constructed once and never change from there onwards.
The only operations should be for building the ModuleGraph
(once and for all -- no update-like/insert-like functions)
and querying the structure in various ways, e.g. to determine reachability.
We should avoid exposing fields like mg_mss since it may be a footgun
trying to use the nodes directly... We do still expose it, but it feels like
all its use cases would be better served by a more proper ModuleGraph
abstraction
Synopsis
- data ModuleGraph = ModuleGraph {
- mg_mss :: [ModuleGraphNode]
- mg_graph :: (ReachabilityIndex SummaryNode, NodeKey -> Maybe SummaryNode)
- mg_loop_graph :: (ReachabilityIndex SummaryNode, NodeKey -> Maybe SummaryNode)
- mg_zero_graph :: (ReachabilityIndex ZeroSummaryNode, ZeroScopeKey -> Maybe ZeroSummaryNode)
- mg_has_holes :: !Bool
- emptyMG :: ModuleGraph
- mkModuleGraph :: [ModuleGraphNode] -> ModuleGraph
- mkModuleGraphChecked :: [ModuleGraphNode] -> Either [ModuleGraphInvariantError] ModuleGraph
- checkModuleGraph :: ModuleGraph -> [ModuleGraphInvariantError]
- data ModuleGraphInvariantError
- data ModuleGraphNode
- mgNodeDependencies :: Bool -> ModuleGraphNode -> [NodeKey]
- mgNodeIsModule :: ModuleGraphNode -> Maybe ModuleNodeInfo
- mgNodeUnitId :: ModuleGraphNode -> UnitId
- data ModuleNodeEdge = ModuleNodeEdge {}
- mkModuleEdge :: ImportLevel -> NodeKey -> ModuleNodeEdge
- mkNormalEdge :: NodeKey -> ModuleNodeEdge
- data ModuleNodeInfo
- moduleNodeInfoModule :: ModuleNodeInfo -> Module
- moduleNodeInfoUnitId :: ModuleNodeInfo -> UnitId
- moduleNodeInfoMnwib :: ModuleNodeInfo -> ModuleNameWithIsBoot
- moduleNodeInfoModuleName :: ModuleNodeInfo -> ModuleName
- moduleNodeInfoModNodeKeyWithUid :: ModuleNodeInfo -> ModNodeKeyWithUid
- moduleNodeInfoHscSource :: ModuleNodeInfo -> Maybe HscSource
- moduleNodeInfoLocation :: ModuleNodeInfo -> ModLocation
- isBootModuleNodeInfo :: ModuleNodeInfo -> IsBootInterface
- lengthMG :: ModuleGraph -> Int
- isEmptyMG :: ModuleGraph -> Bool
- mapMG :: (ModSummary -> ModSummary) -> ModuleGraph -> ModuleGraph
- mgMapM :: (ModuleNodeInfo -> IO ModuleNodeInfo) -> ModuleGraph -> IO ModuleGraph
- mgModSummaries :: ModuleGraph -> [ModSummary]
- mgLookupModule :: ModuleGraph -> Module -> Maybe ModuleNodeInfo
- mgLookupModuleName :: ModuleGraph -> ModuleNameWithIsBoot -> [ModuleNodeInfo]
- mgHasHoles :: ModuleGraph -> Bool
- showModMsg :: DynFlags -> Bool -> ModuleGraphNode -> SDoc
- mgReachable :: ModuleGraph -> NodeKey -> Maybe [ModuleGraphNode]
- mgReachableLoop :: ModuleGraph -> [NodeKey] -> [ModuleGraphNode]
- mgQuery :: ModuleGraph -> NodeKey -> NodeKey -> Bool
- data ZeroScopeKey
- mgQueryZero :: ModuleGraph -> ZeroScopeKey -> ZeroScopeKey -> Bool
- mgQueryMany :: ModuleGraph -> [NodeKey] -> NodeKey -> Bool
- mgQueryManyZero :: ModuleGraph -> [ZeroScopeKey] -> ZeroScopeKey -> Bool
- mgMember :: ModuleGraph -> NodeKey -> Bool
- mgModSummaries' :: ModuleGraph -> [ModuleGraphNode]
- moduleGraphNodes :: Bool -> [ModuleGraphNode] -> (Graph SummaryNode, NodeKey -> Maybe SummaryNode)
- moduleGraphModulesBelow :: ModuleGraph -> UnitId -> ModuleNameWithIsBoot -> Set ModNodeKeyWithUid
- filterToposortToModules :: [SCC ModuleGraphNode] -> [SCC ModuleNodeInfo]
- moduleGraphNodesZero :: [ModuleGraphNode] -> (Graph ZeroSummaryNode, ZeroScopeKey -> Maybe ZeroSummaryNode)
- type StageSummaryNode = Node Int (NodeKey, ModuleStage)
- stageSummaryNodeSummary :: StageSummaryNode -> (NodeKey, ModuleStage)
- stageSummaryNodeKey :: StageSummaryNode -> Int
- mkStageDeps :: [ModuleGraphNode] -> (ReachabilityIndex StageSummaryNode, (NodeKey, ModuleStage) -> Maybe StageSummaryNode)
- data NodeKey
- mkNodeKey :: ModuleGraphNode -> NodeKey
- nodeKeyUnitId :: NodeKey -> UnitId
- nodeKeyModName :: NodeKey -> Maybe ModuleName
- type ModNodeKey = ModuleNameWithIsBoot
- data ModNodeKeyWithUid = ModNodeKeyWithUid {}
- mnkToModule :: ModNodeKeyWithUid -> Module
- moduleToMnk :: Module -> IsBootInterface -> ModNodeKeyWithUid
- mnkToInstalledModule :: ModNodeKeyWithUid -> InstalledModule
- installedModuleToMnk :: InstalledModule -> ModNodeKeyWithUid
- mnkIsBoot :: ModNodeKeyWithUid -> IsBootInterface
- msKey :: ModSummary -> ModNodeKeyWithUid
- mnKey :: ModuleNodeInfo -> ModNodeKeyWithUid
- miKey :: ModIface -> ModNodeKeyWithUid
- data ImportLevel
- type SummaryNode = Node Int ModuleGraphNode
- summaryNodeSummary :: SummaryNode -> ModuleGraphNode
- summaryNodeKey :: SummaryNode -> Int
Construct a module graph
A module graph should be constructed once by downsweep and never modified.
data ModuleGraph #
A 'ModuleGraph' contains all the nodes from the home package (only). See
'ModuleGraphNode' for information about the nodes.
Modules need to be compiled. hs-boots need to be typechecked before the associated "real" module so modules with {-# SOURCE #-} imports can be built. Instantiations also need to be typechecked to ensure that the module fits the signature. Substantiation typechecking is roughly comparable to the check that the module and its hs-boot agree.
The graph is not necessarily stored in topologically-sorted order. Use
topSortModuleGraph and flattenSCC to achieve this.
Constructors
| ModuleGraph | |
Fields
| |
emptyMG :: ModuleGraph #
Why do we ever need to construct empty graphs? Is it because of one shot mode?
mkModuleGraph :: [ModuleGraphNode] -> ModuleGraph #
Construct a module graph. This function should be the only entry point for
building a ModuleGraph, since it is supposed to be built once and never modified.
If you ever find the need to build a ModuleGraph iteratively, don't
add insert and update functions to the API since they become footguns.
Instead, design an API that allows iterative construction without posterior
modification, perhaps like what is done for building arrays from mutable
arrays.
mkModuleGraphChecked :: [ModuleGraphNode] -> Either [ModuleGraphInvariantError] ModuleGraph #
A version of mkModuleGraph that checks the module graph for invariants.
Invariant checking
data ModuleGraphInvariantError #
Constructors
| FixedNodeDependsOnCompileNode ModNodeKeyWithUid [NodeKey] | |
| DuplicateModuleNodeKey NodeKey | |
| DependencyNotInGraph NodeKey [NodeKey] |
Instances
| Outputable ModuleGraphInvariantError # | |
Defined in GHC.Unit.Module.Graph Methods ppr :: ModuleGraphInvariantError -> SDoc # | |
| Eq ModuleGraphInvariantError # | |
Defined in GHC.Unit.Module.Graph Methods (==) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # (/=) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # | |
| Ord ModuleGraphInvariantError # | |
Defined in GHC.Unit.Module.Graph Methods compare :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Ordering # (<) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # (<=) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # (>) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # (>=) :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> Bool # max :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> ModuleGraphInvariantError # min :: ModuleGraphInvariantError -> ModuleGraphInvariantError -> ModuleGraphInvariantError # | |
Nodes in a module graph
The user-facing nodes in a module graph are ModuleGraphNodes.
There are a few things which we can query out of each ModuleGraphNode:
mgNodeDependenciesgets the immediate dependencies of this nodemgNodeUnitIdreturns theUnitIdof that nodemgNodeModSumextracts theModSummaryof a node if exists
data ModuleGraphNode #
A 'ModuleGraphNode' is a node in the 'ModuleGraph'.
Edges between nodes mark dependencies arising from module imports
and dependencies arising from backpack instantiations.
Constructors
| InstantiationNode UnitId InstantiatedUnit | Instantiation nodes track the instantiation of other units (backpack dependencies) with the holes (signatures) of the current package. |
| ModuleNode [ModuleNodeEdge] ModuleNodeInfo | There is a module node for each module being built. A node is either fixed or can be compiled. - Fixed modules are not compiled, the artifacts are just loaded from disk. It is up to your to make sure the artifacts are up to date and available. - Compile modules are compiled from source if needed. |
| LinkNode [NodeKey] UnitId | Link nodes are whether are are creating a linked product (ie executable/shared object etc) for a unit. |
| UnitNode [UnitId] UnitId | Package dependency |
Instances
| Outputable ModuleGraphNode # | |
Defined in GHC.Unit.Module.Graph Methods ppr :: ModuleGraphNode -> SDoc # | |
| Eq ModuleGraphNode # | |
Defined in GHC.Unit.Module.Graph Methods (==) :: ModuleGraphNode -> ModuleGraphNode -> Bool # (/=) :: ModuleGraphNode -> ModuleGraphNode -> Bool # | |
| Ord ModuleGraphNode # | |
Defined in GHC.Unit.Module.Graph Methods compare :: ModuleGraphNode -> ModuleGraphNode -> Ordering # (<) :: ModuleGraphNode -> ModuleGraphNode -> Bool # (<=) :: ModuleGraphNode -> ModuleGraphNode -> Bool # (>) :: ModuleGraphNode -> ModuleGraphNode -> Bool # (>=) :: ModuleGraphNode -> ModuleGraphNode -> Bool # max :: ModuleGraphNode -> ModuleGraphNode -> ModuleGraphNode # min :: ModuleGraphNode -> ModuleGraphNode -> ModuleGraphNode # | |
mgNodeDependencies :: Bool -> ModuleGraphNode -> [NodeKey] #
Collect the immediate dependencies of a ModuleGraphNode, optionally avoiding hs-boot dependencies. If the drop_hs_boot_nodes flag is False, and if this is a .hs and there is an equivalent .hs-boot, add a link from the former to the latter. This has the effect of detecting bogus cases where the .hs-boot depends on the .hs, by introducing a cycle. Additionally, it ensures that we will always process the .hs-boot before the .hs, and so the HomePackageTable will always have the most up to date information.
mgNodeUnitId :: ModuleGraphNode -> UnitId #
data ModuleNodeEdge #
Constructors
| ModuleNodeEdge | |
Fields | |
Instances
| Outputable ModuleNodeEdge # | |
Defined in GHC.Unit.Module.Graph Methods ppr :: ModuleNodeEdge -> SDoc # | |
mkModuleEdge :: ImportLevel -> NodeKey -> ModuleNodeEdge #
mkNormalEdge :: NodeKey -> ModuleNodeEdge #
A normal edge in the graph which isn't offset by an import stage.
data ModuleNodeInfo #
moduleNodeInfoModule :: ModuleNodeInfo -> Module #
Extract the Module from a ModuleNodeInfo
moduleNodeInfoModuleName :: ModuleNodeInfo -> ModuleName #
Extract the ModuleName from a ModuleNodeInfo
moduleNodeInfoModNodeKeyWithUid :: ModuleNodeInfo -> ModNodeKeyWithUid #
Extract the ModNodeKeyWithUid from a ModuleNodeInfo
moduleNodeInfoHscSource :: ModuleNodeInfo -> Maybe HscSource #
Extract the HscSource from a ModuleNodeInfo, if we can determine it.
moduleNodeInfoLocation :: ModuleNodeInfo -> ModLocation #
Extract the ModLocation from a ModuleNodeInfo
isBootModuleNodeInfo :: ModuleNodeInfo -> IsBootInterface #
Extract the IsBootInterface from a ModuleNodeInfo
Module graph operations
lengthMG :: ModuleGraph -> Int #
Returns the number of nodes in a ModuleGraph
isEmptyMG :: ModuleGraph -> Bool #
ModSummary operations
A couple of operations on the module graph allow access to the
ModSummarys of the modules in it contained.
In particular, mapMG and mapMGM allow updating these ModSummarys
(without changing the ModuleGraph structure itself!).
mgModSummaries lists out all ModSummarys, and
mgLookupModule looks up a ModSummary for a given module.
mapMG :: (ModSummary -> ModSummary) -> ModuleGraph -> ModuleGraph #
Map a function f over all the ModSummaries.
To preserve invariants, f can't change the isBoot status.
mgMapM :: (ModuleNodeInfo -> IO ModuleNodeInfo) -> ModuleGraph -> IO ModuleGraph #
Map a function f over all the ModSummaries, in IO.
To preserve invariants, f can't change the isBoot status.
mgModSummaries :: ModuleGraph -> [ModSummary] #
mgLookupModule :: ModuleGraph -> Module -> Maybe ModuleNodeInfo #
Look up a non-boot ModSummary in the ModuleGraph.
Careful: Linear in the size of the module graph MP: This should probably be level aware
mgLookupModuleName :: ModuleGraph -> ModuleNameWithIsBoot -> [ModuleNodeInfo] #
Lookup up a ModuleNameWithIsBoot in the ModuleGraph.
Multiple nodes in the ModuleGraph can have the same ModuleName
and IsBootInterface.
Careful: Linear in the size of the module graph.
mgHasHoles :: ModuleGraph -> Bool #
A function you should not need to use, or desire to use. Only used
in one place, Load to facilitate a misimplementation in Backpack.
showModMsg :: DynFlags -> Bool -> ModuleGraphNode -> SDoc #
Reachability queries
A module graph explains the structure and relationship between the modules being compiled. Often times, this structure is relevant to answer reachability queries -- is X reachable from Y; or, what is the transitive closure of Z?
mgReachable :: ModuleGraph -> NodeKey -> Maybe [ModuleGraphNode] #
Return all nodes reachable from the given NodeKey.
Nothing if the key couldn't be found in the graph.
mgReachableLoop :: ModuleGraph -> [NodeKey] -> [ModuleGraphNode] #
Things which are reachable if hs-boot files are ignored. Used by getLinkDeps
Arguments
| :: ModuleGraph | g |
| -> NodeKey | a |
| -> NodeKey | b |
| -> Bool |
|
Reachability Query.
mgQuery(g, a, b) asks:
Can we reach b from a in graph g?
Both a and b must be in g.
data ZeroScopeKey #
The ZeroScopeKey indicates the different scopes which we can refer to in a zero-scope query.
Constructors
| ModuleScope ModNodeKeyWithUid ImportLevel | |
| UnitScope UnitId |
Instances
| Outputable ZeroScopeKey # | |
Defined in GHC.Unit.Module.Graph Methods ppr :: ZeroScopeKey -> SDoc # | |
| Eq ZeroScopeKey # | |
Defined in GHC.Unit.Module.Graph | |
| Ord ZeroScopeKey # | |
Defined in GHC.Unit.Module.Graph Methods compare :: ZeroScopeKey -> ZeroScopeKey -> Ordering # (<) :: ZeroScopeKey -> ZeroScopeKey -> Bool # (<=) :: ZeroScopeKey -> ZeroScopeKey -> Bool # (>) :: ZeroScopeKey -> ZeroScopeKey -> Bool # (>=) :: ZeroScopeKey -> ZeroScopeKey -> Bool # max :: ZeroScopeKey -> ZeroScopeKey -> ZeroScopeKey # min :: ZeroScopeKey -> ZeroScopeKey -> ZeroScopeKey # | |
mgQueryZero :: ModuleGraph -> ZeroScopeKey -> ZeroScopeKey -> Bool #
answers the question: can we reach mgQueryZero g root bb from root
in the module graph g, only using normal (level 0) imports?
Arguments
| :: ModuleGraph | g |
| -> [NodeKey] | roots |
| -> NodeKey | b |
| -> Bool |
|
Many roots reachability Query.
mgQuery(g, roots, b) asks:
Can we reach b from any of the roots in graph g?
Node b must be in g.
Arguments
| :: ModuleGraph | g |
| -> [ZeroScopeKey] | roots |
| -> ZeroScopeKey | b |
| -> Bool |
|
Many roots reachability Query.
mgQuery(g, roots, b) asks:
Can we reach b from any of the roots in graph g, only using normal (level 0) imports?
Node b must be in g.
mgMember :: ModuleGraph -> NodeKey -> Bool #
Other operations
These operations allow more-internal-than-ideal access to the ModuleGraph structure. Ideally, we could restructure the code using these functions to avoid deconstructing/reconstructing the ModuleGraph and instead extend the "proper interface" of the ModuleGraph to achieve what is currently done but through a better abstraction.
mgModSummaries' :: ModuleGraph -> [ModuleGraphNode] #
moduleGraphNodes :: Bool -> [ModuleGraphNode] -> (Graph SummaryNode, NodeKey -> Maybe SummaryNode) #
Turn a list of graph nodes into an efficient queriable graph. The first boolean parameter indicates whether nodes corresponding to hs-boot files should be collapsed into their relevant hs nodes.
moduleGraphModulesBelow :: ModuleGraph -> UnitId -> ModuleNameWithIsBoot -> Set ModNodeKeyWithUid #
This function returns all the modules belonging to the home-unit that can be reached by following the given dependencies. Additionally, if both the boot module and the non-boot module can be reached, it only returns the non-boot one.
filterToposortToModules :: [SCC ModuleGraphNode] -> [SCC ModuleNodeInfo] #
This function filters out all the instantiation nodes from each SCC of a topological sort. Use this with care, as the resulting "strongly connected components" may not really be strongly connected in a direct way, as instantiations have been removed. It would probably be best to eliminate uses of this function where possible.
moduleGraphNodesZero :: [ModuleGraphNode] -> (Graph ZeroSummaryNode, ZeroScopeKey -> Maybe ZeroSummaryNode) #
Turn a list of graph nodes into an efficient queriable graph. This graph only has edges between level-0 imports
This query answers the question. If I am looking at level n in module M then which modules are visible?
If you are looking at level -1 then the reachable modules are those imported at splice and then any modules those modules import at zero. (Ie the zero scope for those modules)
type StageSummaryNode = Node Int (NodeKey, ModuleStage) #
mkStageDeps :: [ModuleGraphNode] -> (ReachabilityIndex StageSummaryNode, (NodeKey, ModuleStage) -> Maybe StageSummaryNode) #
Transitive dependencies, but with the stage that each module is required at.
Keys into the ModuleGraph
Constructors
| NodeKey_Unit !InstantiatedUnit | |
| NodeKey_Module !ModNodeKeyWithUid | |
| NodeKey_Link !UnitId | |
| NodeKey_ExternalUnit !UnitId |
Instances
| Outputable NodeKey # | |
Defined in GHC.Unit.Module.Graph | |
| Eq NodeKey # | |
| Ord NodeKey # | |
Defined in GHC.Unit.Module.Graph | |
mkNodeKey :: ModuleGraphNode -> NodeKey #
nodeKeyUnitId :: NodeKey -> UnitId #
nodeKeyModName :: NodeKey -> Maybe ModuleName #
type ModNodeKey = ModuleNameWithIsBoot #
data ModNodeKeyWithUid #
Constructors
| ModNodeKeyWithUid | |
Fields | |
Instances
| Outputable ModNodeKeyWithUid # | |
Defined in GHC.Unit.Module.ModNodeKey Methods ppr :: ModNodeKeyWithUid -> SDoc # | |
| Eq ModNodeKeyWithUid # | |
Defined in GHC.Unit.Module.ModNodeKey Methods (==) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # (/=) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # | |
| Ord ModNodeKeyWithUid # | |
Defined in GHC.Unit.Module.ModNodeKey Methods compare :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Ordering # (<) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # (<=) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # (>) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # (>=) :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> Bool # max :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> ModNodeKeyWithUid # min :: ModNodeKeyWithUid -> ModNodeKeyWithUid -> ModNodeKeyWithUid # | |
mnkToModule :: ModNodeKeyWithUid -> Module #
moduleToMnk :: Module -> IsBootInterface -> ModNodeKeyWithUid #
installedModuleToMnk :: InstalledModule -> ModNodeKeyWithUid #
Already InstalledModules are always NotBoot
msKey :: ModSummary -> ModNodeKeyWithUid #
miKey :: ModIface -> ModNodeKeyWithUid #
data ImportLevel #
ImportLevel
Constructors
| NormalLevel | |
| SpliceLevel | |
| QuoteLevel |
Instances
Internal node representation
SummaryNode is the internal representation for each node stored in
the graph. It's not immediately clear to me why users do depend on them.
type SummaryNode = Node Int ModuleGraphNode #
summaryNodeKey :: SummaryNode -> Int #