Add getFunc session accessor.
[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 Flatten
10
11
12 -- | A map from a HsFunction identifier to various stuff we collect about a
13 --   function along the way.
14 type FuncMap  = [(HsFunction, FuncData)]
15 -- | Some stuff we collect about a function along the way.
16 type FuncData = (FlatFunction)
17
18 data VHDLSession = VHDLSession {
19   coreMod   :: HscTypes.CoreModule, -- The current module
20   nameCount :: Int,             -- A counter that can be used to generate unique names
21   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
22 }
23
24 -- | Add the function to the session
25 addFunc :: HsFunction -> FlatFunction -> VHDLState ()
26 addFunc hsfunc flatfunc = do
27   fs <- State.gets funcs -- Get the funcs element from the session
28   State.modify (\x -> x {funcs = (hsfunc, flatfunc) : fs }) -- Prepend name and f
29
30 -- | Find the given function in the current session
31 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
32 getFunc hsfunc = do
33   fs <- State.gets funcs -- Get the funcs element from the session
34   return $ lookup hsfunc fs
35
36 getModule :: VHDLState HscTypes.CoreModule
37 getModule = State.gets coreMod -- Get the coreMod element from the session
38
39 type VHDLState = State.State VHDLSession
40
41 -- Makes the given name unique by appending a unique number.
42 -- This does not do any checking against existing names, so it only guarantees
43 -- uniqueness with other names generated by uniqueName.
44 uniqueName :: String -> VHDLState String
45 uniqueName name = do
46   count <- State.gets nameCount -- Get the funcs element from the session
47   State.modify (\s -> s {nameCount = count + 1})
48   return $ name ++ "_" ++ (show count)
49
50 -- vim: set ts=8 sw=2 sts=2 expandtab: