[project @ 2000-10-31 12:07:43 by simonpj]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsMonad.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
5
6 \begin{code}
7 module DsMonad (
8         DsM,
9         initDs, returnDs, thenDs, andDs, mapDs, listDs,
10         mapAndUnzipDs, zipWithDs, foldlDs,
11         uniqSMtoDsM,
12         newTyVarsDs, cloneTyVarsDs,
13         duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
14         newFailLocalDs,
15         getSrcLocDs, putSrcLocDs,
16         getModuleDs,
17         getUniqueDs,
18         getDOptsDs,
19         dsLookupGlobalValue,
20
21         dsWarn, 
22         DsWarnings,
23         DsMatchContext(..), DsMatchKind(..)
24     ) where
25
26 #include "HsVersions.h"
27
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 )
33 import Outputable
34 import SrcLoc           ( noSrcLoc, SrcLoc )
35 import TcHsSyn          ( TypecheckedPat )
36 import Type             ( Type )
37 import UniqSupply       ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
38                           UniqSM, UniqSupply )
39 import Unique           ( Unique )
40 import Util             ( zipWithEqual )
41 import Name             ( Name )
42 import Name             ( lookupNameEnv )
43 import HscTypes         ( HomeSymbolTable, PersistentCompilerState(..), 
44                           TyThing(..), TypeEnv, lookupType )
45 import CmdLineOpts      ( DynFlags )
46
47 infixr 9 `thenDs`
48 \end{code}
49
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:
53 \begin{code}
54 type DsM result =
55         DynFlags
56         -> UniqSupply
57         -> (Name -> Id)         -- Lookup well-known Ids
58         -> SrcLoc               -- to put in pattern-matching error msgs
59         -> Module               -- module: for SCC profiling
60         -> DsWarnings
61         -> (result, DsWarnings)
62
63 type DsWarnings = Bag WarnMsg           -- The desugarer reports matches which are
64                                         -- completely shadowed or incomplete patterns
65
66 {-# INLINE andDs #-}
67 {-# INLINE thenDs #-}
68 {-# INLINE returnDs #-}
69
70 -- initDs returns the UniqSupply out the end (not just the result)
71
72 initDs  :: DynFlags
73         -> UniqSupply
74         -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
75         -> Module   -- module name: for profiling
76         -> DsM a
77         -> (a, DsWarnings)
78
79 initDs dflags init_us (hst,pcs,local_type_env) mod action
80   = action dflags init_us lookup noSrcLoc mod emptyBag
81   where
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
86     pte = pcs_PTE pcs
87     lookup n = case lookupType hst pte n of {
88                  Just (AnId v) -> v ;
89                  other -> 
90                case lookupNameEnv local_type_env n of
91                  Just (AnId v) -> v ;
92                  other         -> pprPanic "initDS: lookup:" (ppr n)
93                }
94
95 thenDs :: DsM a -> (a -> DsM b) -> DsM b
96 andDs  :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
97
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}}
102
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) }}}
108
109 returnDs :: a -> DsM a
110 returnDs result dflags us genv loc mod warns = (result, warns)
111
112 listDs :: [DsM a] -> DsM [a]
113 listDs []     = returnDs []
114 listDs (x:xs)
115   = x           `thenDs` \ r  ->
116     listDs xs   `thenDs` \ rs ->
117     returnDs (r:rs)
118
119 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
120
121 mapDs f []     = returnDs []
122 mapDs f (x:xs)
123   = f x         `thenDs` \ r  ->
124     mapDs f xs  `thenDs` \ rs ->
125     returnDs (r:rs)
126
127 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
128
129 foldlDs k z []     = returnDs z
130 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
131                      foldlDs k r xs
132
133 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
134
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)
140
141 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
142
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 ->
147     returnDs (r:rs)
148 \end{code}
149
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.
154
155 \begin{code}
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) }
160
161 newSysLocalsDs tys = mapDs newSysLocalDs tys
162
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
167
168 getUniqueDs :: DsM Unique
169 getUniqueDs dflags us genv loc mod warns
170   = case (uniqFromSupply us) of { assigned_uniq ->
171     (assigned_uniq, warns) }
172
173 getDOptsDs :: DsM DynFlags
174 getDOptsDs dflags us genv loc mod warns
175   = (dflags, warns)
176
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) }
181
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) }
186 \end{code}
187
188 \begin{code}
189 newTyVarsDs :: [TyVar] -> DsM [TyVar]
190
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) }
194 \end{code}
195
196 We can also reach out and either set/grab location information from
197 the @SrcLoc@ being carried around.
198 \begin{code}
199 uniqSMtoDsM :: UniqSM a -> DsM a
200
201 uniqSMtoDsM u_action dflags us genv loc mod warns
202   = (initUs_ us u_action, warns)
203
204 getSrcLocDs :: DsM SrcLoc
205 getSrcLocDs dflags us genv loc mod warns
206   = (loc, warns)
207
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
211
212 dsWarn :: WarnMsg -> DsM ()
213 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
214
215 \end{code}
216
217 \begin{code}
218 getModuleDs :: DsM Module
219 getModuleDs dflags us genv loc mod warns = (mod, warns)
220 \end{code}
221
222 \begin{code}
223 dsLookupGlobalValue :: Name -> DsM Id
224 dsLookupGlobalValue name dflags us genv loc mod warns
225   = (genv name, warns)
226 \end{code}
227
228
229 %************************************************************************
230 %*                                                                      *
231 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
232 %*                                                                      *
233 %************************************************************************
234
235 \begin{code}
236 data DsMatchContext
237   = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc
238   | NoMatchContext
239   deriving ()
240
241 data DsMatchKind
242   = FunMatch Id
243   | CaseMatch
244   | LambdaMatch
245   | PatBindMatch
246   | DoBindMatch
247   | ListCompMatch
248   | LetMatch
249   | RecUpdMatch
250   deriving ()
251 \end{code}