[project @ 2004-11-25 11:36:34 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, mappM,
9         initDs, returnDs, thenDs, listDs, fixDs, mapAndUnzipDs, foldlDs,
10
11         newTyVarsDs, 
12         duplicateLocalDs, newSysLocalDs, newSysLocalsDs, newUniqueId,
13         newFailLocalDs,
14         getSrcSpanDs, putSrcSpanDs,
15         getModuleDs,
16         newUnique, 
17         UniqSupply, newUniqueSupply,
18         getDOptsDs,
19         dsLookupGlobal, dsLookupGlobalId, dsLookupTyCon, dsLookupDataCon,
20
21         DsMetaEnv, DsMetaVal(..), dsLookupMetaEnv, dsExtendMetaEnv,
22
23         dsWarn, 
24         DsWarning,
25         DsMatchContext(..)
26     ) where
27
28 #include "HsVersions.h"
29
30 import TcRnMonad
31 import HsSyn            ( HsExpr, HsMatchContext, Pat )
32 import TcIface          ( tcIfaceGlobal )
33 import RdrName          ( GlobalRdrEnv )
34 import HscTypes         ( TyThing(..), TypeEnv, HscEnv, 
35                           tyThingId, tyThingTyCon, tyThingDataCon, unQualInScope )
36 import Bag              ( emptyBag, snocBag, Bag )
37 import DataCon          ( DataCon )
38 import TyCon            ( TyCon )
39 import DataCon          ( DataCon )
40 import Id               ( mkSysLocal, setIdUnique, Id )
41 import Module           ( Module )
42 import Var              ( TyVar, setTyVarUnique )
43 import Outputable
44 import SrcLoc           ( noSrcSpan, SrcSpan )
45 import Type             ( Type )
46 import UniqSupply       ( UniqSupply, uniqsFromSupply )
47 import Name             ( Name, nameOccName )
48 import NameEnv
49 import OccName          ( occNameFS )
50 import CmdLineOpts      ( DynFlags )
51 import ErrUtils         ( WarnMsg, mkWarnMsg )
52 import Bag              ( mapBag )
53
54 import DATA_IOREF       ( newIORef, readIORef )
55
56 infixr 9 `thenDs`
57 \end{code}
58
59 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
60 a @UniqueSupply@ and some annotations, which
61 presumably include source-file location information:
62 \begin{code}
63 type DsM result = TcRnIf DsGblEnv DsLclEnv result
64
65 -- Compatibility functions
66 fixDs    = fixM
67 thenDs   = thenM
68 returnDs = returnM
69 listDs   = sequenceM
70 foldlDs  = foldlM
71 mapAndUnzipDs = mapAndUnzipM
72
73
74 type DsWarning = (SrcSpan, SDoc)
75         -- Not quite the same as a WarnMsg, we have an SDoc here 
76         -- and we'll do the print_unqual stuff later on to turn it
77         -- into a Doc.
78
79 data DsGblEnv = DsGblEnv {
80         ds_mod     :: Module,                   -- For SCC profiling
81         ds_warns   :: IORef (Bag DsWarning),    -- Warning messages
82         ds_if_env  :: IfGblEnv                  -- Used for looking up global, 
83                                                 -- possibly-imported things
84     }
85
86 data DsLclEnv = DsLclEnv {
87         ds_meta    :: DsMetaEnv,        -- Template Haskell bindings
88         ds_loc     :: SrcSpan           -- to put in pattern-matching error msgs
89      }
90
91 -- Inside [| |] brackets, the desugarer looks 
92 -- up variables in the DsMetaEnv
93 type DsMetaEnv = NameEnv DsMetaVal
94
95 data DsMetaVal
96    = Bound Id           -- Bound by a pattern inside the [| |]. 
97                         -- Will be dynamically alpha renamed.
98                         -- The Id has type THSyntax.Var
99
100    | Splice (HsExpr Id) -- These bindings are introduced by
101                         -- the PendingSplices on a HsBracketOut
102
103 -- initDs returns the UniqSupply out the end (not just the result)
104
105 initDs  :: HscEnv
106         -> Module -> GlobalRdrEnv -> TypeEnv
107         -> DsM a
108         -> IO (a, Bag WarnMsg)
109
110 initDs hsc_env mod rdr_env type_env thing_inside
111   = do  { warn_var <- newIORef emptyBag
112         ; let { if_env = IfGblEnv { if_rec_types = Just (mod, return type_env) }
113               ; gbl_env = DsGblEnv { ds_mod = mod, 
114                                      ds_if_env = if_env, 
115                                      ds_warns = warn_var }
116               ; lcl_env = DsLclEnv { ds_meta = emptyNameEnv, 
117                                      ds_loc = noSrcSpan } }
118
119         ; res <- initTcRnIf 'd' hsc_env gbl_env lcl_env thing_inside
120
121         ; warns <- readIORef warn_var
122         ; return (res, mapBag mk_warn warns)
123         }
124    where
125     print_unqual = unQualInScope rdr_env
126
127     mk_warn :: (SrcSpan,SDoc) -> WarnMsg
128     mk_warn (loc,sdoc) = mkWarnMsg loc print_unqual sdoc
129 \end{code}
130
131 And all this mysterious stuff is so we can occasionally reach out and
132 grab one or more names.  @newLocalDs@ isn't exported---exported
133 functions are defined with it.  The difference in name-strings makes
134 it easier to read debugging output.
135
136 \begin{code}
137 -- Make a new Id with the same print name, but different type, and new unique
138 newUniqueId :: Name -> Type -> DsM Id
139 newUniqueId id ty
140   = newUnique   `thenDs` \ uniq ->
141     returnDs (mkSysLocal (occNameFS (nameOccName id)) uniq ty)
142
143 duplicateLocalDs :: Id -> DsM Id
144 duplicateLocalDs old_local 
145   = newUnique   `thenDs` \ uniq ->
146     returnDs (setIdUnique old_local uniq)
147
148 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
149 newSysLocalDs ty
150   = newUnique   `thenDs` \ uniq ->
151     returnDs (mkSysLocal FSLIT("ds") uniq ty)
152
153 newSysLocalsDs tys = mappM newSysLocalDs tys
154
155 newFailLocalDs ty 
156   = newUnique   `thenDs` \ uniq ->
157     returnDs (mkSysLocal FSLIT("fail") uniq ty)
158         -- The UserLocal bit just helps make the code a little clearer
159 \end{code}
160
161 \begin{code}
162 newTyVarsDs :: [TyVar] -> DsM [TyVar]
163 newTyVarsDs tyvar_tmpls 
164   = newUniqueSupply     `thenDs` \ uniqs ->
165     returnDs (zipWith setTyVarUnique tyvar_tmpls (uniqsFromSupply uniqs))
166 \end{code}
167
168 We can also reach out and either set/grab location information from
169 the @SrcSpan@ being carried around.
170
171 \begin{code}
172 getDOptsDs :: DsM DynFlags
173 getDOptsDs = getDOpts
174
175 getModuleDs :: DsM Module
176 getModuleDs = do { env <- getGblEnv; return (ds_mod env) }
177
178 getSrcSpanDs :: DsM SrcSpan
179 getSrcSpanDs = do { env <- getLclEnv; return (ds_loc env) }
180
181 putSrcSpanDs :: SrcSpan -> DsM a -> DsM a
182 putSrcSpanDs new_loc thing_inside = updLclEnv (\ env -> env {ds_loc = new_loc}) thing_inside
183
184 dsWarn :: DsWarning -> DsM ()
185 dsWarn (loc,warn) = do { env <- getGblEnv; updMutVar (ds_warns env) (`snocBag` (loc,msg)) }
186             where
187               msg = ptext SLIT("Warning:") <+> warn
188 \end{code}
189
190 \begin{code}
191 dsLookupGlobal :: Name -> DsM TyThing
192 -- Very like TcEnv.tcLookupGlobal
193 dsLookupGlobal name 
194   = do  { env <- getGblEnv
195         ; setEnvs (ds_if_env env, ())
196                   (tcIfaceGlobal name) }
197
198 dsLookupGlobalId :: Name -> DsM Id
199 dsLookupGlobalId name 
200   = dsLookupGlobal name         `thenDs` \ thing ->
201     returnDs (tyThingId thing)
202
203 dsLookupTyCon :: Name -> DsM TyCon
204 dsLookupTyCon name
205   = dsLookupGlobal name         `thenDs` \ thing ->
206     returnDs (tyThingTyCon thing)
207
208 dsLookupDataCon :: Name -> DsM DataCon
209 dsLookupDataCon name
210   = dsLookupGlobal name         `thenDs` \ thing ->
211     returnDs (tyThingDataCon thing)
212 \end{code}
213
214 \begin{code}
215 dsLookupMetaEnv :: Name -> DsM (Maybe DsMetaVal)
216 dsLookupMetaEnv name = do { env <- getLclEnv; return (lookupNameEnv (ds_meta env) name) }
217
218 dsExtendMetaEnv :: DsMetaEnv -> DsM a -> DsM a
219 dsExtendMetaEnv menv thing_inside
220   = updLclEnv (\env -> env { ds_meta = ds_meta env `plusNameEnv` menv }) thing_inside
221 \end{code}
222
223
224 %************************************************************************
225 %*                                                                      *
226 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
227 %*                                                                      *
228 %************************************************************************
229
230 \begin{code}
231 data DsMatchContext
232   = DsMatchContext (HsMatchContext Name) [Pat Id] SrcSpan
233   | NoMatchContext
234   deriving ()
235 \end{code}