@@ -41,7 +41,17 @@ eliminateDeadCode uber@UberModule {..} =
4141 }
4242 where
4343 preservedForeigns ∷ [(ModuleName , FilePath , NonEmpty Name )]
44- preservedForeigns = uberModuleForeigns
44+ preservedForeigns = do
45+ (modname, path, foreignNames) ← annotatedForeigns
46+ case NE. nonEmpty (preservedNames foreignNames) of
47+ Nothing → []
48+ Just names → [(modname, path, names)]
49+ where
50+ preservedNames ∷ [(Id , Name )] → [Name ]
51+ preservedNames foreignNames = do
52+ (idName, foreignName) ← foreignNames
53+ guard $ idName `Set.member` reachableIds
54+ pure foreignName
4555
4656 preserveBindings ∷ [Grouping (QName , Exp )]
4757 preserveBindings = do
@@ -68,12 +78,15 @@ eliminateDeadCode uber@UberModule {..} =
6878
6979 annotatedExports ∷ [(Id , Name , AExp )]
7080 annotatedBindings ∷ [Grouping (Id , QName , AExp )]
71- (annotatedExports, annotatedBindings) = runAnnM do
81+ annotatedForeigns ∷ [(ModuleName , FilePath , [(Id , Name )])]
82+ (annotatedExports, annotatedBindings, annotatedForeigns) = runAnnM do
7283 annExports ← forM uberModuleExports \ (name, expr) →
7384 (,name,) <$> nextId <*> annotateExp expr
7485 annBindings ← forM uberModuleBindings $ traverse \ (qname, expr) →
7586 (,qname,) <$> nextId <*> annotateExp expr
76- pure (annExports, annBindings)
87+ annForeignNames ← forM uberModuleForeigns \ (modname, path, names) →
88+ (modname,path,) <$> forM (toList names) \ name → fmap (,name) nextId
89+ pure (annExports, annBindings, annForeignNames)
7790
7891 dceAnnotatedExp ∷ AExp → Exp
7992 dceAnnotatedExp =
@@ -128,7 +141,15 @@ eliminateDeadCode uber@UberModule {..} =
128141
129142 adjacencyList ∷ [(() , Id , [Id ])]
130143 adjacencyList =
131- DL. toList $ adjacencyListFromExports <> adjacencyListFromBindings
144+ DL. toList $
145+ adjacencyListFromExports
146+ <> adjacencyListFromBindings
147+ <> adjacencyListFromForeigns
148+
149+ adjacencyListFromExports ∷ DList (() , Id , [Id ])
150+ adjacencyListFromExports =
151+ annotatedExports & foldMap \ (nodeId, _name, expr) →
152+ adjacencyListFromExport nodeId expr
132153
133154 adjacencyListFromBindings ∷ DList (() , Id , [Id ])
134155 adjacencyListFromBindings =
@@ -139,18 +160,22 @@ eliminateDeadCode uber@UberModule {..} =
139160 recBinds & foldMap \ (nodeId, _qname, expr) →
140161 adjacencyListForExpr bindingsInScope (nodeId, expr)
141162
142- adjacencyListFromExports ∷ DList (() , Id , [Id ])
143- adjacencyListFromExports =
144- annotatedExports & foldMap \ (nodeId, _name, expr ) →
145- adjacencyListFromExport nodeId expr
163+ adjacencyListFromForeigns ∷ DList (() , Id , [Id ])
164+ adjacencyListFromForeigns =
165+ annotatedForeigns & foldMap \ (_modname, _path, names ) →
166+ DL. fromList (( () ,, [] ) . fst <$> names)
146167
147168 bindingsInScope ∷ Map (Qualified Name , Index ) Id
148169 bindingsInScope =
149- Map. fromList
150- [ ((Imported m name, 0 ), bindId)
170+ Map. fromList $
171+ [ ((Imported modname name, 0 ), bindId)
151172 | grouping ← annotatedBindings
152- , (bindId, QName m name, _boundExpr) ← listGrouping grouping
173+ , (bindId, QName modname name, _boundExpr) ← listGrouping grouping
153174 ]
175+ <> [ ((Imported modname name, 0 ), foreignNameId)
176+ | (modname, _path, foreignNames) ← annotatedForeigns
177+ , (foreignNameId, name) ← foreignNames
178+ ]
154179
155180 adjacencyListFromExport ∷ Id → AExp → DList (() , Id , [Id ])
156181 adjacencyListFromExport = curry (adjacencyListForExpr bindingsInScope)
0 commit comments