2 -- | Computations in the vectorisation monad concerned with naming
3 -- and fresh variable generation.
4 module Vectorise.Monad.Naming
14 import Vectorise.Monad.Base
26 -- Naming ---------------------------------------------------------------------
27 -- | Clone a name, using the provide function to transform its `OccName`.
28 cloneName :: (OccName -> OccName) -> Name -> VM Name
29 cloneName mk_occ name = liftM make (liftDs newUnique)
31 occ_name = mk_occ (nameOccName name)
33 make u | isExternalName name = mkExternalName u (nameModule name)
36 | otherwise = mkSystemName u occ_name
39 -- | Clone an `Id`, using the provided function to transform its `OccName`.
40 cloneId :: (OccName -> OccName) -> Id -> Type -> VM Id
43 name <- cloneName mk_occ (getName id)
44 let id' | isExportedId id = Id.mkExportedLocalId name ty
45 | otherwise = Id.mkLocalId name ty
49 -- | Make a fresh instance of this var, with a new unique.
50 cloneVar :: Var -> VM Var
51 cloneVar var = liftM (setIdUnique var) (liftDs newUnique)
54 -- | Make a fresh exported variable with the given type.
55 newExportedVar :: OccName -> Type -> VM Var
56 newExportedVar occ_name ty
57 = do mod <- liftDs getModuleDs
60 let name = mkExternalName u mod occ_name noSrcSpan
62 return $ Id.mkExportedLocalId name ty
65 -- | Make a fresh local variable with the given type.
66 -- The variable's name is formed using the given string as the prefix.
67 newLocalVar :: FastString -> Type -> VM Var
69 = do u <- liftDs newUnique
70 return $ mkSysLocal fs u ty
73 -- | Make several fresh local varaiables with the given types.
74 -- The variable's names are formed using the given string as the prefix.
75 newLocalVars :: FastString -> [Type] -> VM [Var]
76 newLocalVars fs = mapM (newLocalVar fs)
79 -- | Make a new local dummy variable.
80 newDummyVar :: Type -> VM Var
81 newDummyVar = newLocalVar (fsLit "vv")
84 -- | Make a fresh type variable with the given kind.
85 -- The variable's name is formed using the given string as the prefix.
86 newTyVar :: FastString -> Kind -> VM Var
88 = do u <- liftDs newUnique
89 return $ mkTyVar (mkSysTvName u fs) k