2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
5 module TranslatorTypes where
7 import qualified Control.Monad.State as State
8 import qualified HscTypes
9 import qualified Data.Map as Map
14 -- | A map from a HsFunction identifier to various stuff we collect about a
15 -- function along the way.
16 type FuncMap = Map.Map HsFunction FuncData
18 -- | Some stuff we collect about a function along the way.
19 data FuncData = FuncData {
20 flatFunc :: Maybe FlatFunction
23 data VHDLSession = VHDLSession {
24 coreMod :: HscTypes.CoreModule, -- The current module
25 nameCount :: Int, -- A counter that can be used to generate unique names
26 funcs :: FuncMap -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
29 -- | Add the function to the session
30 addFunc :: HsFunction -> VHDLState ()
32 fs <- State.gets funcs -- Get the funcs element from the session
33 let fs' = Map.insert hsfunc (FuncData Nothing) fs -- Insert function
34 State.modify (\x -> x {funcs = fs' })
36 -- | Find the given function in the current session
37 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
39 fs <- State.gets funcs -- Get the funcs element from the session
40 return $ Map.lookup hsfunc fs
42 -- | Sets the FlatFunction for the given HsFunction in the given setting.
43 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
44 setFlatFunc hsfunc flatfunc = do
45 fs <- State.gets funcs -- Get the funcs element from the session
46 let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
47 State.modify (\x -> x {funcs = fs' })
49 -- | Modify all functions in the map using the given function
50 modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
52 fs <- State.gets funcs -- Get the funcs element from the session
53 let fs' = Map.mapWithKey f fs
54 State.modify (\x -> x {funcs = fs' })
56 getModule :: VHDLState HscTypes.CoreModule
57 getModule = State.gets coreMod -- Get the coreMod element from the session
59 type VHDLState = State.State VHDLSession
61 -- Makes the given name unique by appending a unique number.
62 -- This does not do any checking against existing names, so it only guarantees
63 -- uniqueness with other names generated by uniqueName.
64 uniqueName :: String -> VHDLState String
66 count <- State.gets nameCount -- Get the funcs element from the session
67 State.modify (\s -> s {nameCount = count + 1})
68 return $ name ++ "_" ++ (show count)
70 -- vim: set ts=8 sw=2 sts=2 expandtab: