Use Data.Accessor for FuncData.
[matthijs/master-project/cλash.git] / TranslatorTypes.hs
index 5d1ecf74343f98332901c974b069d805f858baa1..17a2b9cccafbc47dfd6314d0a5d21159a8e9a665 100644 (file)
@@ -2,10 +2,13 @@
 -- Simple module providing some types used by Translator. These are in a
 -- separate module to prevent circular dependencies in Pretty for example.
 --
+{-# LANGUAGE TemplateHaskell #-}
 module TranslatorTypes where
 
 import qualified Control.Monad.State as State
 import qualified Data.Map as Map
+import qualified Data.Accessor.Template
+import Data.Accessor
 
 import qualified HscTypes
 
@@ -22,10 +25,13 @@ type FuncMap  = Map.Map HsFunction FuncData
 
 -- | Some stuff we collect about a function along the way.
 data FuncData = FuncData {
-  flatFunc     :: Maybe FlatFunction,
-  funcEntity   :: Maybe Entity,
-  funcArch     :: Maybe AST.ArchBody
-}
+  fdFlatFunc_ :: Maybe FlatFunction,
+  fdEntity_ :: Maybe Entity,
+  fdArch_ :: Maybe AST.ArchBody
+} deriving (Show)
+
+-- Derive accessors
+$( Data.Accessor.Template.deriveAccessors ''FuncData )
 
 data VHDLSession = VHDLSession {
   coreMod   :: HscTypes.CoreModule, -- The current module
@@ -50,16 +56,36 @@ getFuncs = do
   fs <- State.gets funcs -- Get the funcs element from the session
   return $ Map.toList fs
 
--- | Sets the FlatFunction for the given HsFunction in the given setting.
+-- | Gets all the functions from the current session
+getHsFuncs :: VHDLState [HsFunction]
+getHsFuncs = do
+  fs <- State.gets funcs -- Get the funcs element from the session
+  return $ Map.keys fs
+  
+-- | Sets the FlatFunction for the given HsFunction in the current session.
 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
 setFlatFunc hsfunc flatfunc =
-  modFunc (\d -> d { flatFunc = Just flatfunc }) hsfunc
+  modFunc (fdFlatFunc ^= Just flatfunc) hsfunc
+
+-- | Sets the Entity for the given HsFunction in the current session.
+setEntity :: HsFunction -> Entity -> VHDLState ()
+setEntity hsfunc entity =
+  modFunc (fdEntity ^= Just entity) hsfunc
+
+-- | Sets the Entity for the given HsFunction in the current session.
+setArchitecture :: HsFunction -> AST.ArchBody -> VHDLState ()
+setArchitecture hsfunc arch =
+  modFunc (fdArch ^= Just arch) hsfunc
 
 -- | Modify a function in the map using the given function
 modFunc :: (FuncData -> FuncData) -> HsFunction -> VHDLState ()
 modFunc f hsfunc =
   modFuncMap (Map.adjust f hsfunc)
 
+-- | Get the map of functions in the session
+getFuncMap :: VHDLState FuncMap
+getFuncMap = State.gets funcs
+
 -- | Modify the function map in the session using the given function
 modFuncMap :: (FuncMap -> FuncMap) -> VHDLState ()
 modFuncMap f = do
@@ -67,10 +93,20 @@ modFuncMap f = do
   let fs' = f fs
   State.modify (\x -> x {funcs = fs' })
 
--- | Modify all functions in the map using the given function
-modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
-modFuncs f =
-  modFuncMap (Map.mapWithKey f)
+-- | Apply the given function to all functions in the map, and collect the
+--   results. The function is allowed to change the function map in the
+--   session, but any new functions added will not be mapped.
+modFuncs :: (HsFunction -> FuncData -> VHDLState ()) -> VHDLState ()
+modFuncs f = do
+  hsfuncs <- getHsFuncs
+  mapM doFunc hsfuncs
+  return ()
+  where
+    doFunc hsfunc = do
+      fdata_maybe <- getFunc hsfunc
+      case fdata_maybe of
+        Nothing -> do return ()
+        Just fdata -> f hsfunc fdata
 
 getModule :: VHDLState HscTypes.CoreModule
 getModule = State.gets coreMod -- Get the coreMod element from the session