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 Data.Map as Map
10 import qualified HscTypes
17 -- | A map from a HsFunction identifier to various stuff we collect about a
18 -- function along the way.
19 type FuncMap = Map.Map HsFunction FuncData
21 -- | Some stuff we collect about a function along the way.
22 data FuncData = FuncData {
23 flatFunc :: Maybe FlatFunction,
24 entity :: Maybe Entity
27 data VHDLSession = VHDLSession {
28 coreMod :: HscTypes.CoreModule, -- The current module
29 nameCount :: Int, -- A counter that can be used to generate unique names
30 funcs :: FuncMap -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
33 -- | Add the function to the session
34 addFunc :: HsFunction -> VHDLState ()
36 fs <- State.gets funcs -- Get the funcs element from the session
37 let fs' = Map.insert hsfunc (FuncData Nothing Nothing) fs -- Insert function
38 State.modify (\x -> x {funcs = fs' })
40 -- | Find the given function in the current session
41 getFunc :: HsFunction -> VHDLState (Maybe FuncData)
43 fs <- State.gets funcs -- Get the funcs element from the session
44 return $ Map.lookup hsfunc fs
46 -- | Gets all functions from the current session
47 getFuncs :: VHDLState [(HsFunction, FuncData)]
49 fs <- State.gets funcs -- Get the funcs element from the session
50 return $ Map.toList fs
52 -- | Sets the FlatFunction for the given HsFunction in the given setting.
53 setFlatFunc :: HsFunction -> FlatFunction -> VHDLState ()
54 setFlatFunc hsfunc flatfunc = do
55 fs <- State.gets funcs -- Get the funcs element from the session
56 let fs'= Map.adjust (\d -> d { flatFunc = Just flatfunc }) hsfunc fs
57 State.modify (\x -> x {funcs = fs' })
59 -- | Modify all functions in the map using the given function
60 modFuncs :: (HsFunction -> FuncData -> FuncData) -> VHDLState ()
62 fs <- State.gets funcs -- Get the funcs element from the session
63 let fs' = Map.mapWithKey f fs
64 State.modify (\x -> x {funcs = fs' })
66 getModule :: VHDLState HscTypes.CoreModule
67 getModule = State.gets coreMod -- Get the coreMod element from the session
69 type VHDLState = State.State VHDLSession
71 -- Makes the given name unique by appending a unique number.
72 -- This does not do any checking against existing names, so it only guarantees
73 -- uniqueness with other names generated by uniqueName.
74 uniqueName :: String -> VHDLState String
76 count <- State.gets nameCount -- Get the funcs element from the session
77 State.modify (\s -> s {nameCount = count + 1})
78 return $ name ++ "_" ++ (show count)
80 -- vim: set ts=8 sw=2 sts=2 expandtab: