@@ -7,11 +7,16 @@ module HsBindgen.Backend.HsModule.Translation (
77 , ImportListItem (.. )
88 -- * HsModule
99 , HsModule (.. )
10+ -- * Binding category selection
11+ , SDeclPredicate
12+ , useSafeCategory
13+ , useUnsafeCategory
14+ , useAllCategories
15+ , selectModuleMultiple
1016 -- * Translation
1117 , defHsModuleName
1218 , translateModuleMultiple
1319 , translateModuleSingle
14- , mergeDecls
1520 ) where
1621
1722import Data.Foldable qualified as Foldable
@@ -65,6 +70,46 @@ data HsModule = HsModule {
6570 , hsModuleDecls :: [SDecl ]
6671 }
6772
73+ {- ------------------------------------------------------------------------------
74+ Binding category selection
75+ -------------------------------------------------------------------------------}
76+
77+ -- TODO_PR: Use `Hs.Name` (but then we need `ImpredicativeTypes`).
78+ type SDeclPredicate = Text -> Bool
79+
80+ useSafeCategory , useUnsafeCategory , useAllCategories :: ByCategory SDeclPredicate
81+ useSafeCategory = ByCategory $ Map. singleton BUnsafe (const False )
82+ useUnsafeCategory = ByCategory $ Map. singleton BSafe (const False )
83+ useAllCategories = ByCategory Map. empty
84+
85+ -- TODO_PR: Double check the select function.
86+ selectCategory ::
87+ SDeclPredicate
88+ -> SDecl
89+ -> Bool
90+ selectCategory p = \ case
91+ (DVar x) -> p $ Hs. getName x. varName
92+ (DInst _) -> True
93+ (DRecord x) -> p $ Hs. getName x. dataType
94+ (DNewtype x) -> p $ Hs. getName x. newtypeName
95+ (DEmptyData x) -> p $ Hs. getName x. emptyDataName
96+ (DDerivingInstance _) -> True
97+ (DForeignImport x) -> p $ Hs. getName x. foreignImportName
98+ (DFunction x) -> p $ Hs. getName x. functionName
99+ (DPatternSynonym x) -> p $ Hs. getName x. patSynName
100+ (DPragma (NOINLINE n)) -> p $ Hs. getName n
101+
102+ -- Default to selecting all declarations.
103+ getPredicate :: BindingCategory -> ByCategory SDeclPredicate -> SDeclPredicate
104+ getPredicate x = fromMaybe (const True ) . Map. lookup x . unByCategory
105+
106+ selectModuleMultiple ::
107+ ByCategory SDeclPredicate
108+ -> ByCategory ([UserlandCapiWrapper ], [SDecl ])
109+ -> ByCategory ([UserlandCapiWrapper ], [SDecl ])
110+ selectModuleMultiple predByCat = mapByCategory $ \ cat (wrappers, decls) ->
111+ (wrappers, filter (selectCategory $ getPredicate cat predByCat) decls)
112+
68113{- ------------------------------------------------------------------------------
69114 Translation
70115-------------------------------------------------------------------------------}
@@ -74,46 +119,30 @@ defHsModuleName = "Generated"
74119
75120translateModuleMultiple ::
76121 Hs. ModuleName
122+ -> ByCategory SDeclPredicate
77123 -> ByCategory ([UserlandCapiWrapper ], [SDecl ])
78124 -> ByCategory HsModule
79- translateModuleMultiple moduleBaseName declsByCat =
80- mapByCategory go declsByCat
125+ translateModuleMultiple moduleBaseName predByCat declsByCat =
126+ mapByCategory go $ selectModuleMultiple predByCat declsByCat
81127 where
82128 go :: BindingCategory -> ([UserlandCapiWrapper ], [SDecl ]) -> HsModule
83- go cat (wrappers, decls) =
84- translateModule' (Just cat) moduleBaseName wrappers decls
129+ go cat xs = translateModule' (Just cat) moduleBaseName xs
85130
86131translateModuleSingle ::
87- Safety
88- -> Hs. ModuleName
132+ Hs. ModuleName
133+ -> ByCategory SDeclPredicate
89134 -> ByCategory ([UserlandCapiWrapper ], [SDecl ])
90135 -> HsModule
91- translateModuleSingle safety name declsByCat =
92- translateModule' Nothing name wrappers decls
93- where
94- wrappers :: [UserlandCapiWrapper ]
95- decls :: [SDecl ]
96- (wrappers, decls) = mergeDecls safety declsByCat
97-
98- mergeDecls ::
99- Safety
100- -> ByCategory ([UserlandCapiWrapper ], [SDecl ])
101- -> ([UserlandCapiWrapper ], [SDecl ])
102- mergeDecls safety declsByCat =
103- Foldable. fold $ ByCategory $ removeSafetyCategory $ unByCategory declsByCat
104- where
105- safetyToRemove = case safety of
106- Safe -> BUnsafe
107- Unsafe -> BSafe
108- removeSafetyCategory = Map. filterWithKey (\ k _ -> k /= safetyToRemove)
136+ translateModuleSingle name predByCat declsByCat =
137+ translateModule' Nothing name $ Foldable. fold $
138+ selectModuleMultiple predByCat declsByCat
109139
110140translateModule' ::
111141 Maybe BindingCategory
112142 -> Hs. ModuleName
113- -> [UserlandCapiWrapper ]
114- -> [SDecl ]
143+ -> ([UserlandCapiWrapper ], [SDecl ])
115144 -> HsModule
116- translateModule' mcat moduleBaseName hsModuleUserlandCapiWrappers hsModuleDecls =
145+ translateModule' mcat moduleBaseName ( hsModuleUserlandCapiWrappers, hsModuleDecls) =
117146 let hsModulePragmas =
118147 resolvePragmas hsModuleUserlandCapiWrappers hsModuleDecls
119148 hsModuleImports =
0 commit comments