2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
9 initDs, returnDs, thenDs, andDs, mapDs, listDs,
10 mapAndUnzipDs, zipWithDs, foldlDs,
12 newTyVarsDs, cloneTyVarsDs,
13 duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
15 getSrcLocDs, putSrcLocDs,
23 DsMatchContext(..), DsMatchKind(..)
26 #include "HsVersions.h"
28 import Bag ( emptyBag, snocBag, Bag )
29 import ErrUtils ( WarnMsg )
30 import Id ( mkSysLocal, setIdUnique, Id )
31 import Module ( Module )
32 import Var ( TyVar, setTyVarUnique )
34 import SrcLoc ( noSrcLoc, SrcLoc )
35 import TcHsSyn ( TypecheckedPat )
37 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
39 import Unique ( Unique )
40 import Util ( zipWithEqual )
41 import Name ( Name, lookupNameEnv )
42 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..),
43 TyThing(..), TypeEnv, lookupTypeEnv )
44 import CmdLineOpts ( DynFlags )
49 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
50 a @UniqueSupply@ and some annotations, which
51 presumably include source-file location information:
56 -> (Name -> Id) -- Lookup well-known Ids
57 -> SrcLoc -- to put in pattern-matching error msgs
58 -> Module -- module: for SCC profiling
60 -> (result, DsWarnings)
62 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
63 -- completely shadowed or incomplete patterns
67 {-# INLINE returnDs #-}
69 -- initDs returns the UniqSupply out the end (not just the result)
73 -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
74 -> Module -- module name: for profiling
78 initDs dflags init_us (hst,pcs,local_type_env) mod action
79 = action dflags init_us lookup noSrcLoc mod emptyBag
81 -- This lookup is used for well-known Ids,
82 -- such as fold, build, cons etc, so the chances are
83 -- it'll be found in the package symbol table. That's
84 -- why we don't merge all these tables
86 lookup n = case lookupTypeEnv pst n of {
89 case lookupTypeEnv hst n of {
92 case lookupNameEnv local_type_env n of
94 other -> pprPanic "initDS: lookup:" (ppr n)
97 thenDs :: DsM a -> (a -> DsM b) -> DsM b
98 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
100 thenDs m1 m2 dflags us genv loc mod warns
101 = case splitUniqSupply us of { (s1, s2) ->
102 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
103 m2 result dflags s2 genv loc mod warns1}}
105 andDs combiner m1 m2 dflags us genv loc mod warns
106 = case splitUniqSupply us of { (s1, s2) ->
107 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
108 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
109 (combiner result1 result2, warns2) }}}
111 returnDs :: a -> DsM a
112 returnDs result dflags us genv loc mod warns = (result, warns)
114 listDs :: [DsM a] -> DsM [a]
115 listDs [] = returnDs []
118 listDs xs `thenDs` \ rs ->
121 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
123 mapDs f [] = returnDs []
125 = f x `thenDs` \ r ->
126 mapDs f xs `thenDs` \ rs ->
129 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
131 foldlDs k z [] = returnDs z
132 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
135 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
137 mapAndUnzipDs f [] = returnDs ([], [])
138 mapAndUnzipDs f (x:xs)
139 = f x `thenDs` \ (r1, r2) ->
140 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
141 returnDs (r1:rs1, r2:rs2)
143 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
145 zipWithDs f [] ys = returnDs []
146 zipWithDs f (x:xs) (y:ys)
147 = f x y `thenDs` \ r ->
148 zipWithDs f xs ys `thenDs` \ rs ->
152 And all this mysterious stuff is so we can occasionally reach out and
153 grab one or more names. @newLocalDs@ isn't exported---exported
154 functions are defined with it. The difference in name-strings makes
155 it easier to read debugging output.
158 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
159 newSysLocalDs ty dflags us genv loc mod warns
160 = case uniqFromSupply us of { assigned_uniq ->
161 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
163 newSysLocalsDs tys = mapDs newSysLocalDs tys
165 newFailLocalDs ty dflags us genv loc mod warns
166 = case uniqFromSupply us of { assigned_uniq ->
167 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
168 -- The UserLocal bit just helps make the code a little clearer
170 getUniqueDs :: DsM Unique
171 getUniqueDs dflags us genv loc mod warns
172 = case (uniqFromSupply us) of { assigned_uniq ->
173 (assigned_uniq, warns) }
175 getDOptsDs :: DsM DynFlags
176 getDOptsDs dflags us genv loc mod warns
179 duplicateLocalDs :: Id -> DsM Id
180 duplicateLocalDs old_local dflags us genv loc mod warns
181 = case uniqFromSupply us of { assigned_uniq ->
182 (setIdUnique old_local assigned_uniq, warns) }
184 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
185 cloneTyVarsDs tyvars dflags us genv loc mod warns
186 = case uniqsFromSupply (length tyvars) us of { uniqs ->
187 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
191 newTyVarsDs :: [TyVar] -> DsM [TyVar]
193 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
194 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
195 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
198 We can also reach out and either set/grab location information from
199 the @SrcLoc@ being carried around.
201 uniqSMtoDsM :: UniqSM a -> DsM a
203 uniqSMtoDsM u_action dflags us genv loc mod warns
204 = (initUs_ us u_action, warns)
206 getSrcLocDs :: DsM SrcLoc
207 getSrcLocDs dflags us genv loc mod warns
210 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
211 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
212 = expr dflags us genv new_loc mod warns
214 dsWarn :: WarnMsg -> DsM ()
215 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
220 getModuleDs :: DsM Module
221 getModuleDs dflags us genv loc mod warns = (mod, warns)
225 dsLookupGlobalValue :: Name -> DsM Id
226 dsLookupGlobalValue name dflags us genv loc mod warns
231 %************************************************************************
233 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
235 %************************************************************************
239 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc