Replace FuncMap by a Data.Map.
[matthijs/master-project/cλash.git] / TranslatorTypes.hs
1 --
2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
4 --
5 module TranslatorTypes where
6
7 import qualified Control.Monad.State as State
8 import qualified HscTypes
9 import qualified Data.Map as Map
10 import Flatten
11
12
13 -- | A map from a HsFunction identifier to various stuff we collect about a
14 --   function along the way.
15 type FuncMap  = Map.Map HsFunction FuncData
16 -- | Some stuff we collect about a function along the way.
17 type FuncData = (FlatFunction)
18
19 data VHDLSession = VHDLSession {
20   coreMod   :: HscTypes.CoreModule, -- The current module
21   nameCount :: Int,             -- A counter that can be used to generate unique names
22   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
23 }
24
25 -- | Add the function to the session
26 addFunc :: HsFunction -> FlatFunction -> VHDLState ()
27 addFunc hsfunc flatfunc = do
28   fs <- State.gets funcs -- Get the funcs element from the session
29   let fs' = Map.insert hsfunc (flatfunc) fs -- Insert function
30   State.modify (\x -> x {funcs = fs' })
31
32 -- | Find the given function in the current session
33 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
34 getFunc hsfunc = do
35   fs <- State.gets funcs -- Get the funcs element from the session
36   return $ Map.lookup hsfunc fs
37
38 getModule :: VHDLState HscTypes.CoreModule
39 getModule = State.gets coreMod -- Get the coreMod element from the session
40
41 type VHDLState = State.State VHDLSession
42
43 -- Makes the given name unique by appending a unique number.
44 -- This does not do any checking against existing names, so it only guarantees
45 -- uniqueness with other names generated by uniqueName.
46 uniqueName :: String -> VHDLState String
47 uniqueName name = do
48   count <- State.gets nameCount -- Get the funcs element from the session
49   State.modify (\s -> s {nameCount = count + 1})
50   return $ name ++ "_" ++ (show count)
51
52 -- vim: set ts=8 sw=2 sts=2 expandtab: