{-# LANGUAGE NamedFieldPuns #-}
module Fay.Compiler.State where
import Fay.Compiler.Misc
import Fay.Compiler.QName
import qualified Fay.Exts.NoAnnotation as N
import Fay.Types
import qualified Data.Map as M
import Data.Set (Set)
import qualified Data.Set as S
import Language.Haskell.Names (sv_origName, Symbols (Symbols), SymValueInfo (SymValue, SymMethod, SymSelector, SymConstructor), OrigName, sv_typeName)
getNonLocalExportsWithoutNewtypes :: N.ModuleName -> CompileState -> Maybe (Set N.QName)
getNonLocalExportsWithoutNewtypes modName cs =
fmap ( S.filter (not . isLocal)
. S.map (origName2QName . sv_origName)
. S.filter (not . (`isNewtype` cs))
. (\(Symbols exports _) -> exports)
)
. M.lookup modName . stateInterfaces $ cs
where
isLocal = (Just modName ==) . qModName
getLocalExportsWithoutNewtypes :: N.ModuleName -> CompileState -> Maybe (Set N.QName)
getLocalExportsWithoutNewtypes modName cs =
fmap ( S.filter isLocal
. S.map (origName2QName . sv_origName)
. S.filter (not . (`isNewtype` cs))
. (\(Symbols exports _) -> exports)
)
. M.lookup modName . stateInterfaces $ cs
where
isLocal = (Just modName ==) . qModName
isNewtype :: SymValueInfo OrigName -> CompileState -> Bool
isNewtype s cs = case s of
SymValue{} -> False
SymMethod{} -> False
SymSelector { sv_typeName = tn } -> not . (`isNewtypeDest` cs) . origName2QName $ tn
SymConstructor { sv_typeName = tn } -> not . (`isNewtypeCons` cs) . origName2QName $ tn
isNewtypeDest :: N.QName -> CompileState -> Bool
isNewtypeDest o = any (\(_,mdest,_) -> mdest == Just o) . stateNewtypes
isNewtypeCons :: N.QName -> CompileState -> Bool
isNewtypeCons o = any (\(cons,_,_) -> cons == o) . stateNewtypes
addModulePath :: ModulePath -> CompileState -> CompileState
addModulePath mp cs = cs { stateJsModulePaths = mp `S.insert` stateJsModulePaths cs }
addedModulePath :: ModulePath -> CompileState -> Bool
addedModulePath mp CompileState { stateJsModulePaths } = mp `S.member` stateJsModulePaths
findTypeSig :: N.QName -> CompileState -> Maybe N.Type
findTypeSig n = M.lookup n . stateTypeSigs