@@ -1,57 +1,60 @@
1module Transform.Interface (filterExports) where
1module Transform.Interface (filterExports) where
3import qualified Data.Map as Map
3import qualified Data.Map as Map
4+import qualified Data.Maybe as Maybe
4import qualified AST.Module as Module
5import qualified AST.Module as Module
5−import qualified AST.Type as Type
6import qualified AST.Variable as Var
6import qualified AST.Variable as Var
8filterExports :: Module.Interface -> Module.Interface
9filterExports :: Module.Interface -> Module.Interface
9filterExports interface =
10filterExports interface =
11 { Module.iTypes =
12 { Module.iTypes =
12− Map.fromList (concatMap getTypes exportedValues)
13+ Map.fromList $
15+ (get (Module.iTypes interface))
16+ (Var.getValues exports ++ ctors)
13 , Module.iAliases =
18 , Module.iAliases =
14− Map.fromList (concatMap getAliases exportedValues)
19+ Map.fromList $
21+ (get (Module.iAliases interface))
22+ (Var.getAliases exports)
15 , Module.iAdts =
24 , Module.iAdts =
16− Map.fromList (concatMap getAdts exportedValues)
25+ Map.fromList
26+ (Maybe.mapMaybe (trimUnions interface) unions)
19− exportedValues :: [Var.Value]
29+ exports :: [Var.Value]
20− exportedValues = Module.iExports interface
31+ Module.iExports interface
22− get :: Map.Map String a -> String -> [(String, a)]
33+ unions :: [(String, Var.Listing String)]
23− get dict x =
34+ unions =
24− case Map.lookup x dict of
35+ Var.getUnions exports
26− Nothing -> []
37+ ctors :: [String]
28− getTypes :: Var.Value -> [(String, Type.CanonicalType)]
39+ concatMap (\(_, Var.Listing ctorList _) -> ctorList) unions
31− Var.Value x -> getType x
42+get :: Map.Map String a -> String -> Maybe (String, a)
32− Var.Alias _ -> []
43+get dict key =
33− Var.Union _ (Var.Listing ctors _) -> concatMap getType ctors
44+ case Map.lookup key dict of
34−
45+ Nothing -> Nothing
35− getType :: String -> [(String, Type.CanonicalType)]
46+ Just value -> Just (key, value)
37− get (Module.iTypes interface) name
48+
39− getAliases :: Var.Value -> [(String, ([String], Type.CanonicalType))]
50+ :: Module.Interface
40− getAliases value =
51+ -> (String, Var.Listing String)
43− Var.Alias name -> get (Module.iAliases interface) name
46− getAdts :: Var.Value -> [(String, Module.AdtInfo String)]
52+ -> Maybe (String, Module.AdtInfo String)
51− Var.Union name (Var.Listing exportedCtors _) ->
53+trimUnions interface (name, Var.Listing exportedCtors _) =
52− case Map.lookup name (Module.iAdts interface) of
54+ case Map.lookup name (Module.iAdts interface) of
53− Nothing -> []
55+ Nothing -> Nothing
54− Just (tvars, ctors) ->
56+ Just (tvars, ctors) ->
57+ let isExported (ctor, _) =
58+ ctor `elem` exportedCtors
55− [(name, (tvars, filter isExported ctors))]
60+ Just (name, (tvars, filter isExported ctors))
57− isExported (ctor, _) = ctor `elem` exportedCtors