2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
9 initDs, returnDs, thenDs, listDs, fixDs, mapAndUnzipDs, foldlDs,
12 duplicateLocalDs, newSysLocalDs, newSysLocalsDs, newUniqueId,
14 getSrcSpanDs, putSrcSpanDs,
17 UniqSupply, newUniqueSupply,
19 dsLookupGlobal, dsLookupGlobalId, dsLookupTyCon, dsLookupDataCon,
21 DsMetaEnv, DsMetaVal(..), dsLookupMetaEnv, dsExtendMetaEnv,
28 EquationInfo(..), MatchResult(..),
32 #include "HsVersions.h"
35 import CoreSyn ( CoreExpr )
36 import HsSyn ( HsExpr, HsMatchContext, Pat )
37 import TcIface ( tcIfaceGlobal )
38 import RdrName ( GlobalRdrEnv )
39 import HscTypes ( TyThing(..), TypeEnv, HscEnv,
40 tyThingId, tyThingTyCon, tyThingDataCon, unQualInScope )
41 import Bag ( emptyBag, snocBag, Bag )
42 import DataCon ( DataCon )
43 import TyCon ( TyCon )
44 import DataCon ( DataCon )
45 import Id ( mkSysLocal, setIdUnique, Id )
46 import Module ( Module )
47 import Var ( TyVar, setTyVarUnique )
49 import SrcLoc ( noSrcSpan, SrcSpan )
51 import UniqSupply ( UniqSupply, uniqsFromSupply )
52 import Name ( Name, nameOccName )
54 import OccName ( occNameFS )
55 import CmdLineOpts ( DynFlags )
56 import ErrUtils ( WarnMsg, mkWarnMsg )
59 import DATA_IOREF ( newIORef, readIORef )
64 %************************************************************************
66 Data types for the desugarer
68 %************************************************************************
72 = DsMatchContext (HsMatchContext Name) [Pat Id] SrcSpan
77 = EqnInfo { eqn_pats :: [Pat Id], -- The patterns for an eqn
78 eqn_rhs :: MatchResult } -- What to do after match
80 -- The semantics of (match vs (EqnInfo wrap pats rhs)) is the MatchResult
81 -- \fail. wrap (case vs of { pats -> rhs fail })
82 -- where vs are not in the domain of wrap
85 -- A MatchResult is an expression with a hole in it
88 CanItFail -- Tells whether the failure expression is used
89 (CoreExpr -> DsM CoreExpr)
90 -- Takes a expression to plug in at the
91 -- failure point(s). The expression should
94 data CanItFail = CanFail | CantFail
96 orFail CantFail CantFail = CantFail
101 %************************************************************************
105 %************************************************************************
107 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
108 a @UniqueSupply@ and some annotations, which
109 presumably include source-file location information:
111 type DsM result = TcRnIf DsGblEnv DsLclEnv result
113 -- Compatibility functions
119 mapAndUnzipDs = mapAndUnzipM
122 type DsWarning = (SrcSpan, SDoc)
123 -- Not quite the same as a WarnMsg, we have an SDoc here
124 -- and we'll do the print_unqual stuff later on to turn it
127 data DsGblEnv = DsGblEnv {
128 ds_mod :: Module, -- For SCC profiling
129 ds_warns :: IORef (Bag DsWarning), -- Warning messages
130 ds_if_env :: (IfGblEnv, IfLclEnv) -- Used for looking up global,
131 -- possibly-imported things
134 data DsLclEnv = DsLclEnv {
135 ds_meta :: DsMetaEnv, -- Template Haskell bindings
136 ds_loc :: SrcSpan -- to put in pattern-matching error msgs
139 -- Inside [| |] brackets, the desugarer looks
140 -- up variables in the DsMetaEnv
141 type DsMetaEnv = NameEnv DsMetaVal
144 = Bound Id -- Bound by a pattern inside the [| |].
145 -- Will be dynamically alpha renamed.
146 -- The Id has type THSyntax.Var
148 | Splice (HsExpr Id) -- These bindings are introduced by
149 -- the PendingSplices on a HsBracketOut
151 -- initDs returns the UniqSupply out the end (not just the result)
154 -> Module -> GlobalRdrEnv -> TypeEnv
156 -> IO (a, Bag WarnMsg)
158 initDs hsc_env mod rdr_env type_env thing_inside
159 = do { warn_var <- newIORef emptyBag
160 ; let { if_genv = IfGblEnv { if_rec_types = Just (mod, return type_env) }
161 ; if_lenv = mkIfLclEnv mod (ptext SLIT("GHC error in desugarer lookup in") <+> ppr mod)
162 ; gbl_env = DsGblEnv { ds_mod = mod,
163 ds_if_env = (if_genv, if_lenv),
164 ds_warns = warn_var }
165 ; lcl_env = DsLclEnv { ds_meta = emptyNameEnv,
166 ds_loc = noSrcSpan } }
168 ; res <- initTcRnIf 'd' hsc_env gbl_env lcl_env thing_inside
170 ; warns <- readIORef warn_var
171 ; return (res, mapBag mk_warn warns)
174 print_unqual = unQualInScope rdr_env
176 mk_warn :: (SrcSpan,SDoc) -> WarnMsg
177 mk_warn (loc,sdoc) = mkWarnMsg loc print_unqual sdoc
180 %************************************************************************
182 Operations in the monad
184 %************************************************************************
186 And all this mysterious stuff is so we can occasionally reach out and
187 grab one or more names. @newLocalDs@ isn't exported---exported
188 functions are defined with it. The difference in name-strings makes
189 it easier to read debugging output.
192 -- Make a new Id with the same print name, but different type, and new unique
193 newUniqueId :: Name -> Type -> DsM Id
195 = newUnique `thenDs` \ uniq ->
196 returnDs (mkSysLocal (occNameFS (nameOccName id)) uniq ty)
198 duplicateLocalDs :: Id -> DsM Id
199 duplicateLocalDs old_local
200 = newUnique `thenDs` \ uniq ->
201 returnDs (setIdUnique old_local uniq)
203 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
205 = newUnique `thenDs` \ uniq ->
206 returnDs (mkSysLocal FSLIT("ds") uniq ty)
208 newSysLocalsDs tys = mappM newSysLocalDs tys
211 = newUnique `thenDs` \ uniq ->
212 returnDs (mkSysLocal FSLIT("fail") uniq ty)
213 -- The UserLocal bit just helps make the code a little clearer
217 newTyVarsDs :: [TyVar] -> DsM [TyVar]
218 newTyVarsDs tyvar_tmpls
219 = newUniqueSupply `thenDs` \ uniqs ->
220 returnDs (zipWith setTyVarUnique tyvar_tmpls (uniqsFromSupply uniqs))
223 We can also reach out and either set/grab location information from
224 the @SrcSpan@ being carried around.
227 getDOptsDs :: DsM DynFlags
228 getDOptsDs = getDOpts
230 getModuleDs :: DsM Module
231 getModuleDs = do { env <- getGblEnv; return (ds_mod env) }
233 getSrcSpanDs :: DsM SrcSpan
234 getSrcSpanDs = do { env <- getLclEnv; return (ds_loc env) }
236 putSrcSpanDs :: SrcSpan -> DsM a -> DsM a
237 putSrcSpanDs new_loc thing_inside = updLclEnv (\ env -> env {ds_loc = new_loc}) thing_inside
239 dsWarn :: DsWarning -> DsM ()
240 dsWarn (loc,warn) = do { env <- getGblEnv; updMutVar (ds_warns env) (`snocBag` (loc,msg)) }
242 msg = ptext SLIT("Warning:") <+> warn
246 dsLookupGlobal :: Name -> DsM TyThing
247 -- Very like TcEnv.tcLookupGlobal
249 = do { env <- getGblEnv
250 ; setEnvs (ds_if_env env)
251 (tcIfaceGlobal name) }
253 dsLookupGlobalId :: Name -> DsM Id
254 dsLookupGlobalId name
255 = dsLookupGlobal name `thenDs` \ thing ->
256 returnDs (tyThingId thing)
258 dsLookupTyCon :: Name -> DsM TyCon
260 = dsLookupGlobal name `thenDs` \ thing ->
261 returnDs (tyThingTyCon thing)
263 dsLookupDataCon :: Name -> DsM DataCon
265 = dsLookupGlobal name `thenDs` \ thing ->
266 returnDs (tyThingDataCon thing)
270 dsLookupMetaEnv :: Name -> DsM (Maybe DsMetaVal)
271 dsLookupMetaEnv name = do { env <- getLclEnv; return (lookupNameEnv (ds_meta env) name) }
273 dsExtendMetaEnv :: DsMetaEnv -> DsM a -> DsM a
274 dsExtendMetaEnv menv thing_inside
275 = updLclEnv (\env -> env { ds_meta = ds_meta env `plusNameEnv` menv }) thing_inside