2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 @DsMonad@: monadery used in desugaring
10 DsM, mapM, mapAndUnzipM,
11 initDs, initDsTc, fixDs,
12 foldlM, foldrM, ifOptM,
13 Applicative(..),(<$>),
15 newTyVarsDs, newLocalName,
16 duplicateLocalDs, newSysLocalDs, newSysLocalsDs, newUniqueId,
18 getSrcSpanDs, putSrcSpanDs,
21 UniqSupply, newUniqueSupply,
22 getDOptsDs, getGhcModeDs, doptDs,
23 dsLookupGlobal, dsLookupGlobalId, dsLookupTyCon, dsLookupDataCon,
26 DsMetaEnv, DsMetaVal(..), dsLookupMetaEnv, dsExtendMetaEnv,
29 DsWarning, warnDs, failWithDs,
33 EquationInfo(..), MatchResult(..), DsWrapper, idDsWrapper,
37 #include "HsVersions.h"
66 %************************************************************************
68 Data types for the desugarer
70 %************************************************************************
74 = DsMatchContext (HsMatchContext Name) SrcSpan
79 = EqnInfo { eqn_pats :: [Pat Id], -- The patterns for an eqn
80 eqn_rhs :: MatchResult } -- What to do after match
82 type DsWrapper = CoreExpr -> CoreExpr
83 idDsWrapper :: DsWrapper
86 -- The semantics of (match vs (EqnInfo wrap pats rhs)) is the MatchResult
87 -- \fail. wrap (case vs of { pats -> rhs fail })
88 -- where vs are not bound by wrap
91 -- A MatchResult is an expression with a hole in it
94 CanItFail -- Tells whether the failure expression is used
95 (CoreExpr -> DsM CoreExpr)
96 -- Takes a expression to plug in at the
97 -- failure point(s). The expression should
100 data CanItFail = CanFail | CantFail
102 orFail :: CanItFail -> CanItFail -> CanItFail
103 orFail CantFail CantFail = CantFail
108 %************************************************************************
112 %************************************************************************
114 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
115 a @UniqueSupply@ and some annotations, which
116 presumably include source-file location information:
118 type DsM result = TcRnIf DsGblEnv DsLclEnv result
120 -- Compatibility functions
121 fixDs :: (a -> DsM a) -> DsM a
124 type DsWarning = (SrcSpan, SDoc)
125 -- Not quite the same as a WarnMsg, we have an SDoc here
126 -- and we'll do the print_unqual stuff later on to turn it
129 data DsGblEnv = DsGblEnv {
130 ds_mod :: Module, -- For SCC profiling
131 ds_unqual :: PrintUnqualified,
132 ds_msgs :: IORef Messages, -- Warning messages
133 ds_if_env :: (IfGblEnv, IfLclEnv) -- Used for looking up global,
134 -- possibly-imported things
137 data DsLclEnv = DsLclEnv {
138 ds_meta :: DsMetaEnv, -- Template Haskell bindings
139 ds_loc :: SrcSpan -- to put in pattern-matching error msgs
142 -- Inside [| |] brackets, the desugarer looks
143 -- up variables in the DsMetaEnv
144 type DsMetaEnv = NameEnv DsMetaVal
147 = Bound Id -- Bound by a pattern inside the [| |].
148 -- Will be dynamically alpha renamed.
149 -- The Id has type THSyntax.Var
151 | Splice (HsExpr Id) -- These bindings are introduced by
152 -- the PendingSplices on a HsBracketOut
155 -> Module -> GlobalRdrEnv -> TypeEnv
158 -- Print errors and warnings, if any arise
160 initDs hsc_env mod rdr_env type_env thing_inside
161 = do { msg_var <- newIORef (emptyBag, emptyBag)
162 ; let dflags = hsc_dflags hsc_env
163 ; (ds_gbl_env, ds_lcl_env) <- mkDsEnvs dflags mod rdr_env type_env msg_var
165 ; either_res <- initTcRnIf 'd' hsc_env ds_gbl_env ds_lcl_env $
166 tryM thing_inside -- Catch exceptions (= errors during desugaring)
168 -- Display any errors and warnings
169 -- Note: if -Werror is used, we don't signal an error here.
170 ; msgs <- readIORef msg_var
171 ; printErrorsAndWarnings dflags msgs
173 ; let final_res | errorsFound dflags msgs = Nothing
174 | otherwise = case either_res of
175 Right res -> Just res
176 Left exn -> pprPanic "initDs" (text (show exn))
177 -- The (Left exn) case happens when the thing_inside throws
178 -- a UserError exception. Then it should have put an error
179 -- message in msg_var, so we just discard the exception
183 initDsTc :: DsM a -> TcM a
184 initDsTc thing_inside
185 = do { this_mod <- getModule
186 ; tcg_env <- getGblEnv
187 ; msg_var <- getErrsVar
189 ; let type_env = tcg_type_env tcg_env
190 rdr_env = tcg_rdr_env tcg_env
191 ; ds_envs <- liftIO $ mkDsEnvs dflags this_mod rdr_env type_env msg_var
192 ; setEnvs ds_envs thing_inside }
194 mkDsEnvs :: DynFlags -> Module -> GlobalRdrEnv -> TypeEnv -> IORef Messages -> IO (DsGblEnv, DsLclEnv)
195 mkDsEnvs dflags mod rdr_env type_env msg_var
196 = do -- TODO: unnecessarily monadic
197 let if_genv = IfGblEnv { if_rec_types = Just (mod, return type_env) }
198 if_lenv = mkIfLclEnv mod (ptext SLIT("GHC error in desugarer lookup in") <+> ppr mod)
199 gbl_env = DsGblEnv { ds_mod = mod,
200 ds_if_env = (if_genv, if_lenv),
201 ds_unqual = mkPrintUnqualified dflags rdr_env,
203 lcl_env = DsLclEnv { ds_meta = emptyNameEnv,
206 return (gbl_env, lcl_env)
210 %************************************************************************
212 Operations in the monad
214 %************************************************************************
216 And all this mysterious stuff is so we can occasionally reach out and
217 grab one or more names. @newLocalDs@ isn't exported---exported
218 functions are defined with it. The difference in name-strings makes
219 it easier to read debugging output.
222 -- Make a new Id with the same print name, but different type, and new unique
223 newUniqueId :: Name -> Type -> DsM Id
224 newUniqueId id ty = do
226 return (mkSysLocal (occNameFS (nameOccName id)) uniq ty)
228 duplicateLocalDs :: Id -> DsM Id
229 duplicateLocalDs old_local = do
231 return (setIdUnique old_local uniq)
233 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
234 newSysLocalDs ty = do
236 return (mkSysLocal FSLIT("ds") uniq ty)
238 newSysLocalsDs :: [Type] -> DsM [Id]
239 newSysLocalsDs tys = mapM newSysLocalDs tys
241 newFailLocalDs ty = do
243 return (mkSysLocal FSLIT("fail") uniq ty)
244 -- The UserLocal bit just helps make the code a little clearer
248 newTyVarsDs :: [TyVar] -> DsM [TyVar]
249 newTyVarsDs tyvar_tmpls = do
250 uniqs <- newUniqueSupply
251 return (zipWith setTyVarUnique tyvar_tmpls (uniqsFromSupply uniqs))
254 We can also reach out and either set/grab location information from
255 the @SrcSpan@ being carried around.
258 getDOptsDs :: DsM DynFlags
259 getDOptsDs = getDOpts
261 doptDs :: DynFlag -> TcRnIf gbl lcl Bool
264 getGhcModeDs :: DsM GhcMode
265 getGhcModeDs = getDOptsDs >>= return . ghcMode
267 getModuleDs :: DsM Module
268 getModuleDs = do { env <- getGblEnv; return (ds_mod env) }
270 getSrcSpanDs :: DsM SrcSpan
271 getSrcSpanDs = do { env <- getLclEnv; return (ds_loc env) }
273 putSrcSpanDs :: SrcSpan -> DsM a -> DsM a
274 putSrcSpanDs new_loc thing_inside = updLclEnv (\ env -> env {ds_loc = new_loc}) thing_inside
276 warnDs :: SDoc -> DsM ()
277 warnDs warn = do { env <- getGblEnv
278 ; loc <- getSrcSpanDs
279 ; let msg = mkWarnMsg loc (ds_unqual env)
280 (ptext SLIT("Warning:") <+> warn)
281 ; updMutVar (ds_msgs env) (\ (w,e) -> (w `snocBag` msg, e)) }
284 failWithDs :: SDoc -> DsM a
286 = do { env <- getGblEnv
287 ; loc <- getSrcSpanDs
288 ; let msg = mkErrMsg loc (ds_unqual env) err
289 ; updMutVar (ds_msgs env) (\ (w,e) -> (w, e `snocBag` msg))
295 dsLookupGlobal :: Name -> DsM TyThing
296 -- Very like TcEnv.tcLookupGlobal
298 = do { env <- getGblEnv
299 ; setEnvs (ds_if_env env)
300 (tcIfaceGlobal name) }
302 dsLookupGlobalId :: Name -> DsM Id
303 dsLookupGlobalId name
304 = tyThingId <$> dsLookupGlobal name
306 dsLookupTyCon :: Name -> DsM TyCon
308 = tyThingTyCon <$> dsLookupGlobal name
310 dsLookupDataCon :: Name -> DsM DataCon
312 = tyThingDataCon <$> dsLookupGlobal name
314 dsLookupClass :: Name -> DsM Class
316 = tyThingClass <$> dsLookupGlobal name
320 dsLookupMetaEnv :: Name -> DsM (Maybe DsMetaVal)
321 dsLookupMetaEnv name = do { env <- getLclEnv; return (lookupNameEnv (ds_meta env) name) }
323 dsExtendMetaEnv :: DsMetaEnv -> DsM a -> DsM a
324 dsExtendMetaEnv menv thing_inside
325 = updLclEnv (\env -> env { ds_meta = ds_meta env `plusNameEnv` menv }) thing_inside