1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.DeadCodeElimination
4-- Copyright : (c) 2014 Phil Freeman
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module Language.PureScript.DeadCodeElimination (
23import Data.Maybe (mapMaybe)
25import Language.PureScript.Names
26import Language.PureScript.Declarations
29-- Eliminate all declarations which are not a transitive dependency of the entry point module
31eliminateDeadCode :: [ModuleName] -> [Module] -> [Module]
32eliminateDeadCode entryPoints ms = map go ms
34 go (Module moduleName ds (Just exps)) = Module moduleName ds' (Just exps')
36 ds' = filter (isUsed moduleName graph vertexFor entryPointVertices) ds
37 exps' = mapMaybe (filterExport ds') exps
38 go _ = error "Exports should have been elaborated in name desugaring"
39 declarations = concatMap declarationsByModule ms
40 (graph, _, vertexFor) = graphFromEdges $ map (\(key, deps) -> (key, key, deps)) declarations
41 entryPointVertices = mapMaybe (vertexFor . fst) . filter (\((mn, _), _) -> mn `elem` entryPoints) $ declarations
43 filterExport :: [Declaration] -> DeclarationRef -> Maybe DeclarationRef
44 filterExport decls r@(TypeRef name _) | (any $ typeExists name) decls = Just r
45 filterExport decls r@(ValueRef name) | (any $ valueExists name) decls = Just r
46 filterExport decls r@(TypeInstanceRef name) | (any $ valueExists name) decls = Just r
47 filterExport _ _ = Nothing
49 valueExists :: Ident -> Declaration -> Bool
50 valueExists name (ValueDeclaration name' _ _ _ _) = name == name'
51 valueExists name (ExternDeclaration _ name' _ _) = name == name'
52 valueExists name (BindingGroupDeclaration decls) = any (\(name', _, _) -> name == name') decls
53 valueExists name (PositionedDeclaration _ d) = valueExists name d
54 valueExists _ _ = False
56 typeExists :: ProperName -> Declaration -> Bool
57 typeExists name (DataDeclaration name' _ _) = name == name'
58 typeExists name (DataBindingGroupDeclaration decls) = any (typeExists name) decls
59 typeExists name (PositionedDeclaration _ d) = typeExists name d
60 typeExists _ _ = False
62type Key = (ModuleName, Either Ident ProperName)
64declarationsByModule :: Module -> [(Key, [Key])]
65declarationsByModule (Module moduleName ds _) = concatMap go ds
67 go :: Declaration -> [(Key, [Key])]
68 go d@(ValueDeclaration name _ _ _ _) = [((moduleName, Left name), dependencies moduleName d)]
69 go (DataDeclaration _ _ dctors) = map (\(name, _) -> ((moduleName, Right name), [])) dctors
70 go (ExternDeclaration _ name _ _) = [((moduleName, Left name), [])]
71 go d@(BindingGroupDeclaration names') = map (\(name, _, _) -> ((moduleName, Left name), dependencies moduleName d)) names'
72 go (DataBindingGroupDeclaration ds') = concatMap go ds'
73 go (PositionedDeclaration _ d) = go d
76dependencies :: (Data d) => ModuleName -> d -> [Key]
77dependencies moduleName = nub . everything (++) (mkQ [] values)
79 values :: Value -> [Key]
80 values (Var ident) = let (mn, name) = qualify moduleName ident in [(mn, Left name)]
81 values (Constructor (Qualified (Just mn) name)) = [(mn, Right name)]
82 values (Constructor (Qualified Nothing _)) = error "Found unqualified data constructor"
85isUsed :: ModuleName -> Graph -> (Key -> Maybe Vertex) -> [Vertex] -> Declaration -> Bool
86isUsed moduleName graph vertexFor entryPointVertices (ValueDeclaration name _ _ _ _) =
87 let Just v' = vertexFor (moduleName, Left name)
88 in any (\v -> path graph v v') entryPointVertices
89isUsed moduleName graph vertexFor entryPointVertices (FixityDeclaration _ name) =
90 let Just v' = vertexFor (moduleName, Left $ Op name)
91 in any (\v -> path graph v v') entryPointVertices
92isUsed moduleName graph vertexFor entryPointVertices (DataDeclaration _ _ dctors) =
93 any (\(pn, _) -> let Just v' = vertexFor (moduleName, Right pn)
94 in any (\v -> path graph v v') entryPointVertices) dctors
95isUsed moduleName graph vertexFor entryPointVertices (ExternDeclaration _ name _ _) =
96 let Just v' = vertexFor (moduleName, Left name)
97 in any (\v -> path graph v v') entryPointVertices
98isUsed moduleName graph vertexFor entryPointVertices (BindingGroupDeclaration ds) =
99 any (\(name, _, _) -> let Just v' = vertexFor (moduleName, Left name)
100 in any (\v -> path graph v v') entryPointVertices) ds
101isUsed moduleName graph vertexFor entryPointVertices (DataBindingGroupDeclaration ds) =
102 any (isUsed moduleName graph vertexFor entryPointVertices) ds
103isUsed moduleName graph vertexFor entryPointVertices (PositionedDeclaration _ d) =
104 isUsed moduleName graph vertexFor entryPointVertices d
105isUsed _ _ _ _ _ = True
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list