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 )
42 import Name ( lookupNameEnv )
43 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..),
44 TyThing(..), TypeEnv, lookupType )
45 import CmdLineOpts ( DynFlags )
50 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
51 a @UniqueSupply@ and some annotations, which
52 presumably include source-file location information:
57 -> (Name -> Id) -- Lookup well-known Ids
58 -> SrcLoc -- to put in pattern-matching error msgs
59 -> Module -- module: for SCC profiling
61 -> (result, DsWarnings)
63 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
64 -- completely shadowed or incomplete patterns
68 {-# INLINE returnDs #-}
70 -- initDs returns the UniqSupply out the end (not just the result)
74 -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
75 -> Module -- module name: for profiling
79 initDs dflags init_us (hst,pcs,local_type_env) mod action
80 = action dflags init_us lookup noSrcLoc mod emptyBag
82 -- This lookup is used for well-known Ids,
83 -- such as fold, build, cons etc, so the chances are
84 -- it'll be found in the package symbol table. That's
85 -- why we don't merge all these tables
87 lookup n = case lookupType hst pte n of {
90 case lookupNameEnv local_type_env n of
92 other -> pprPanic "initDS: lookup:" (ppr n)
95 thenDs :: DsM a -> (a -> DsM b) -> DsM b
96 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
98 thenDs m1 m2 dflags us genv loc mod warns
99 = case splitUniqSupply us of { (s1, s2) ->
100 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
101 m2 result dflags s2 genv loc mod warns1}}
103 andDs combiner m1 m2 dflags us genv loc mod warns
104 = case splitUniqSupply us of { (s1, s2) ->
105 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
106 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
107 (combiner result1 result2, warns2) }}}
109 returnDs :: a -> DsM a
110 returnDs result dflags us genv loc mod warns = (result, warns)
112 listDs :: [DsM a] -> DsM [a]
113 listDs [] = returnDs []
116 listDs xs `thenDs` \ rs ->
119 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
121 mapDs f [] = returnDs []
123 = f x `thenDs` \ r ->
124 mapDs f xs `thenDs` \ rs ->
127 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
129 foldlDs k z [] = returnDs z
130 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
133 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
135 mapAndUnzipDs f [] = returnDs ([], [])
136 mapAndUnzipDs f (x:xs)
137 = f x `thenDs` \ (r1, r2) ->
138 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
139 returnDs (r1:rs1, r2:rs2)
141 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
143 zipWithDs f [] ys = returnDs []
144 zipWithDs f (x:xs) (y:ys)
145 = f x y `thenDs` \ r ->
146 zipWithDs f xs ys `thenDs` \ rs ->
150 And all this mysterious stuff is so we can occasionally reach out and
151 grab one or more names. @newLocalDs@ isn't exported---exported
152 functions are defined with it. The difference in name-strings makes
153 it easier to read debugging output.
156 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
157 newSysLocalDs ty dflags us genv loc mod warns
158 = case uniqFromSupply us of { assigned_uniq ->
159 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
161 newSysLocalsDs tys = mapDs newSysLocalDs tys
163 newFailLocalDs ty dflags us genv loc mod warns
164 = case uniqFromSupply us of { assigned_uniq ->
165 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
166 -- The UserLocal bit just helps make the code a little clearer
168 getUniqueDs :: DsM Unique
169 getUniqueDs dflags us genv loc mod warns
170 = case (uniqFromSupply us) of { assigned_uniq ->
171 (assigned_uniq, warns) }
173 getDOptsDs :: DsM DynFlags
174 getDOptsDs dflags us genv loc mod warns
177 duplicateLocalDs :: Id -> DsM Id
178 duplicateLocalDs old_local dflags us genv loc mod warns
179 = case uniqFromSupply us of { assigned_uniq ->
180 (setIdUnique old_local assigned_uniq, warns) }
182 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
183 cloneTyVarsDs tyvars dflags us genv loc mod warns
184 = case uniqsFromSupply (length tyvars) us of { uniqs ->
185 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
189 newTyVarsDs :: [TyVar] -> DsM [TyVar]
191 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
192 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
193 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
196 We can also reach out and either set/grab location information from
197 the @SrcLoc@ being carried around.
199 uniqSMtoDsM :: UniqSM a -> DsM a
201 uniqSMtoDsM u_action dflags us genv loc mod warns
202 = (initUs_ us u_action, warns)
204 getSrcLocDs :: DsM SrcLoc
205 getSrcLocDs dflags us genv loc mod warns
208 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
209 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
210 = expr dflags us genv new_loc mod warns
212 dsWarn :: WarnMsg -> DsM ()
213 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
218 getModuleDs :: DsM Module
219 getModuleDs dflags us genv loc mod warns = (mod, warns)
223 dsLookupGlobalValue :: Name -> DsM Id
224 dsLookupGlobalValue name dflags us genv loc mod warns
229 %************************************************************************
231 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
233 %************************************************************************
237 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc