[project @ 1999-06-23 10:38:13 by simonmar]
[ghc-hetmet.git] / ghc / compiler / parser / RdrHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[RdrHsSyn]{Specialisations of the @HsSyn@ syntax for the reader}
5
6 (Well, really, for specialisations involving @RdrName@s, even if
7 they are used somewhat later on in the compiler...)
8
9 \begin{code}
10 module RdrHsSyn (
11         RdrNameArithSeqInfo,
12         RdrNameBangType,
13         RdrNameClassOpSig,
14         RdrNameConDecl,
15         RdrNameConDetails,
16         RdrNameContext,
17         RdrNameSpecDataSig,
18         RdrNameDefaultDecl,
19         RdrNameForeignDecl,
20         RdrNameGRHS,
21         RdrNameGRHSs,
22         RdrNameHsBinds,
23         RdrNameHsDecl,
24         RdrNameHsExpr,
25         RdrNameHsModule,
26         RdrNameIE,
27         RdrNameImportDecl,
28         RdrNameInstDecl,
29         RdrNameMatch,
30         RdrNameMonoBinds,
31         RdrNamePat,
32         RdrNameHsType,
33         RdrNameHsTyVar,
34         RdrNameSig,
35         RdrNameStmt,
36         RdrNameTyClDecl,
37         RdrNameRuleDecl,
38         RdrNameRuleBndr,
39         RdrNameHsRecordBinds,
40
41         RdrBinding(..),
42         RdrMatch(..),
43         SigConverter,
44
45         RdrNameClassOpPragmas,
46         RdrNameClassPragmas,
47         RdrNameDataPragmas,
48         RdrNameGenPragmas,
49         RdrNameInstancePragmas,
50         extractHsTyRdrNames, 
51         extractHsTyRdrTyVars,
52         extractPatsTyVars, 
53         extractRuleBndrsTyVars,
54  
55         mkOpApp, mkClassDecl, mkClassOpSig,
56
57         cvBinds,
58         cvMonoBindsAndSigs,
59         cvTopDecls,
60         cvValSig, cvClassOpSig, cvInstDeclSig
61     ) where
62
63 #include "HsVersions.h"
64
65 import HsSyn
66 import Name             ( mkClassTyConOcc, mkClassDataConOcc )
67 import OccName          ( mkClassTyConOcc, mkClassDataConOcc, 
68                           mkSuperDictSelOcc, mkDefaultMethodOcc
69                         )
70 import RdrName          ( RdrName, isRdrTyVar, mkRdrUnqual, rdrNameOcc )
71 import Util             ( thenCmp )
72 import HsPragmas        
73 import List             ( nub )
74 import BasicTypes       ( RecFlag(..) )
75 import Outputable
76 \end{code}
77
78  
79 %************************************************************************
80 %*                                                                      *
81 \subsection{Type synonyms}
82 %*                                                                      *
83 %************************************************************************
84
85 \begin{code}
86 type RdrNameArithSeqInfo        = ArithSeqInfo          RdrName RdrNamePat
87 type RdrNameBangType            = BangType              RdrName
88 type RdrNameClassOpSig          = Sig                   RdrName
89 type RdrNameConDecl             = ConDecl               RdrName
90 type RdrNameConDetails          = ConDetails            RdrName
91 type RdrNameContext             = Context               RdrName
92 type RdrNameHsDecl              = HsDecl                RdrName RdrNamePat
93 type RdrNameSpecDataSig         = SpecDataSig           RdrName
94 type RdrNameDefaultDecl         = DefaultDecl           RdrName
95 type RdrNameForeignDecl         = ForeignDecl           RdrName
96 type RdrNameGRHS                = GRHS                  RdrName RdrNamePat
97 type RdrNameGRHSs               = GRHSs                 RdrName RdrNamePat
98 type RdrNameHsBinds             = HsBinds               RdrName RdrNamePat
99 type RdrNameHsExpr              = HsExpr                RdrName RdrNamePat
100 type RdrNameHsModule            = HsModule              RdrName RdrNamePat
101 type RdrNameIE                  = IE                    RdrName
102 type RdrNameImportDecl          = ImportDecl            RdrName
103 type RdrNameInstDecl            = InstDecl              RdrName RdrNamePat
104 type RdrNameMatch               = Match                 RdrName RdrNamePat
105 type RdrNameMonoBinds           = MonoBinds             RdrName RdrNamePat
106 type RdrNamePat                 = InPat                 RdrName
107 type RdrNameHsType              = HsType                RdrName
108 type RdrNameHsTyVar             = HsTyVar               RdrName
109 type RdrNameSig                 = Sig                   RdrName
110 type RdrNameStmt                = Stmt                  RdrName RdrNamePat
111 type RdrNameTyClDecl            = TyClDecl              RdrName RdrNamePat
112 type RdrNameRuleBndr            = RuleBndr              RdrName
113 type RdrNameRuleDecl            = RuleDecl              RdrName RdrNamePat
114
115 type RdrNameHsRecordBinds       = HsRecordBinds         RdrName RdrNamePat
116
117 type RdrNameClassOpPragmas      = ClassOpPragmas        RdrName
118 type RdrNameClassPragmas        = ClassPragmas          RdrName
119 type RdrNameDataPragmas         = DataPragmas           RdrName
120 type RdrNameGenPragmas          = GenPragmas            RdrName
121 type RdrNameInstancePragmas     = InstancePragmas       RdrName
122 \end{code}
123
124
125 %************************************************************************
126 %*                                                                      *
127 \subsection{A few functions over HsSyn at RdrName}
128 %*                                                                    *
129 %************************************************************************
130
131 @extractHsTyRdrNames@ finds the free variables of a HsType
132 It's used when making the for-alls explicit.
133
134 \begin{code}
135 extractHsTyRdrNames :: HsType RdrName -> [RdrName]
136 extractHsTyRdrNames ty = nub (extract_ty ty [])
137
138 extractHsTyRdrTyVars     :: RdrNameHsType -> [RdrName]
139 extractHsTyRdrTyVars ty =  filter isRdrTyVar (extractHsTyRdrNames ty)
140
141 extractRuleBndrsTyVars :: [RuleBndr RdrName] -> [RdrName]
142 extractRuleBndrsTyVars bndrs = filter isRdrTyVar (nub (foldr go [] bndrs))
143                            where
144                              go (RuleBndr _)       acc = acc
145                              go (RuleBndrSig _ ty) acc = extract_ty ty acc
146
147 extractHsCtxtRdrNames :: Context RdrName -> [RdrName]
148 extractHsCtxtRdrNames ty = nub (extract_ctxt ty [])
149
150 extract_ctxt ctxt acc = foldr extract_ass acc ctxt
151                     where
152                       extract_ass (cls, tys) acc = foldr extract_ty (cls : acc) tys
153
154 extract_ty (MonoTyApp ty1 ty2)          acc = extract_ty ty1 (extract_ty ty2 acc)
155 extract_ty (MonoListTy ty)              acc = extract_ty ty acc
156 extract_ty (MonoTupleTy tys _)          acc = foldr extract_ty acc tys
157 extract_ty (MonoFunTy ty1 ty2)          acc = extract_ty ty1 (extract_ty ty2 acc)
158 extract_ty (MonoDictTy cls tys)         acc = foldr extract_ty (cls : acc) tys
159 extract_ty (MonoUsgTy usg ty)           acc = extract_ty ty acc
160 extract_ty (MonoTyVar tv)               acc = tv : acc
161 extract_ty (HsForAllTy Nothing ctxt ty) acc = extract_ctxt ctxt (extract_ty ty acc)
162 extract_ty (HsForAllTy (Just tvs) ctxt ty) 
163                                 acc = acc ++
164                                       (filter (`notElem` locals) $
165                                        extract_ctxt ctxt (extract_ty ty []))
166                                     where
167                                       locals = map getTyVarName tvs
168
169
170 extractPatsTyVars :: [RdrNamePat] -> [RdrName]
171 extractPatsTyVars pats = filter isRdrTyVar (nub (foldr extract_pat [] pats))
172
173 extract_pat (SigPatIn pat ty)      acc = extract_ty ty acc
174 extract_pat WildPatIn              acc = acc
175 extract_pat (VarPatIn var)         acc = acc
176 extract_pat (LitPatIn _)           acc = acc
177 extract_pat (LazyPatIn pat)        acc = extract_pat pat acc
178 extract_pat (AsPatIn a pat)        acc = extract_pat pat acc
179 extract_pat (NPlusKPatIn n _)      acc = acc
180 extract_pat (ConPatIn c pats)      acc = foldr extract_pat acc pats
181 extract_pat (ConOpPatIn p1 c f p2) acc = extract_pat p1 (extract_pat p2 acc)
182 extract_pat (NegPatIn  pat)        acc = extract_pat pat acc
183 extract_pat (ParPatIn  pat)        acc = extract_pat pat acc
184 extract_pat (ListPatIn pats)       acc = foldr extract_pat acc pats
185 extract_pat (TuplePatIn pats _)    acc = foldr extract_pat acc pats
186 extract_pat (RecPatIn c fields)    acc = foldr (\ (f,pat,_) acc -> extract_pat pat acc) acc fields
187 \end{code}
188
189 mkClassDecl builds a RdrClassDecl, filling in the names for tycon and datacon
190 by deriving them from the name of the class.  We fill in the names for the
191 tycon and datacon corresponding to the class, by deriving them from the
192 name of the class itself.  This saves recording the names in the interface
193 file (which would be equally good).
194
195 Similarly for mkClassOpSig and default-method names.
196   
197 \begin{code}
198 mkClassDecl cxt cname tyvars sigs mbinds prags loc
199   = ClassDecl cxt cname tyvars sigs mbinds prags tname dname sc_sel_names loc
200   where
201     cls_occ = rdrNameOcc cname
202     dname   = mkRdrUnqual (mkClassDataConOcc cls_occ)
203     tname   = mkRdrUnqual (mkClassTyConOcc   cls_occ)
204     sc_sel_names = [ mkRdrUnqual (mkSuperDictSelOcc n cls_occ) 
205                    | n <- [1..length cxt]]
206       -- We number off the superclass selectors, 1, 2, 3 etc so that we 
207       -- can construct names for the selectors.  Thus
208       --      class (C a, C b) => D a b where ...
209       -- gives superclass selectors
210       --      D_sc1, D_sc2
211       -- (We used to call them D_C, but now we can have two different
212       --  superclasses both called C!)
213
214 mkClassOpSig has_default_method op ty loc
215   | not has_default_method = ClassOpSig op Nothing    ty loc
216   | otherwise              = ClassOpSig op (Just dm_rn) ty loc
217   where
218     dm_rn = mkRdrUnqual (mkDefaultMethodOcc (rdrNameOcc op))
219 \end{code}
220
221 A useful function for building @OpApps@.  The operator is always a variable,
222 and we don't know the fixity yet.
223
224 \begin{code}
225 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
226 \end{code}
227
228 %************************************************************************
229 %*                                                                      *
230 \subsection[rdrBinding]{Bindings straight out of the parser}
231 %*                                                                      *
232 %************************************************************************
233
234 \begin{code}
235 data RdrBinding
236   =   -- On input we use the Empty/And form rather than a list
237     RdrNullBind
238   | RdrAndBindings    RdrBinding RdrBinding
239
240       -- Value bindings havn't been united with their
241       -- signatures yet
242   | RdrValBinding     RdrNameMonoBinds
243
244       -- Signatures are mysterious; we can't
245       -- tell if its a Sig or a ClassOpSig,
246       -- so we just save the pieces:
247   | RdrSig            RdrNameSig
248
249       -- The remainder all fit into the main HsDecl form
250   | RdrHsDecl         RdrNameHsDecl
251   
252 type SigConverter = RdrNameSig -> RdrNameSig
253 \end{code}
254
255 \begin{code}
256 data RdrMatch
257   = RdrMatch
258              [RdrNamePat]
259              (Maybe RdrNameHsType)
260              RdrNameGRHSs
261 \end{code}
262
263 %************************************************************************
264 %*                                                                      *
265 \subsection[cvDecls]{Convert various top-level declarations}
266 %*                                                                      *
267 %************************************************************************
268
269 We make a point not to throw any user-pragma ``sigs'' at
270 these conversion functions:
271
272 \begin{code}
273 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
274
275 cvValSig      sig = sig
276
277 cvInstDeclSig sig = sig
278
279 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
280 cvClassOpSig sig                       = sig
281 \end{code}
282
283
284 %************************************************************************
285 %*                                                                      *
286 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
287 %*                                                                      *
288 %************************************************************************
289
290 Function definitions are restructured here. Each is assumed to be recursive
291 initially, and non recursive definitions are discovered by the dependency
292 analyser.
293
294 \begin{code}
295 cvBinds :: SigConverter -> RdrBinding -> RdrNameHsBinds
296         -- The mysterious SigConverter converts Sigs to ClassOpSigs
297         -- in class declarations.  Mostly it's just an identity function
298
299 cvBinds sig_cvtr binding
300   = case (cvMonoBindsAndSigs sig_cvtr binding) of { (mbs, sigs) ->
301     MonoBind mbs sigs Recursive
302     }
303 \end{code}
304
305 \begin{code}
306 cvMonoBindsAndSigs :: SigConverter
307                    -> RdrBinding
308                    -> (RdrNameMonoBinds, [RdrNameSig])
309
310 cvMonoBindsAndSigs sig_cvtr fb
311   = mangle_bind (EmptyMonoBinds, []) fb
312   where
313     mangle_bind acc RdrNullBind
314       = acc
315
316     mangle_bind acc (RdrAndBindings fb1 fb2)
317       = mangle_bind (mangle_bind acc fb1) fb2
318
319     mangle_bind (b_acc, s_acc) (RdrSig sig)
320       = (b_acc, sig_cvtr sig : s_acc)
321
322     mangle_bind (b_acc, s_acc) (RdrValBinding binding)
323       = (b_acc `AndMonoBinds` binding, s_acc)
324 \end{code}
325
326
327 %************************************************************************
328 %*                                                                      *
329 \subsection[PrefixToHS-utils]{Utilities for conversion}
330 %*                                                                      *
331 %************************************************************************
332
333 Separate declarations into all the various kinds:
334
335 \begin{code}
336 cvTopDecls :: RdrBinding -> [RdrNameHsDecl]
337 cvTopDecls bind
338   = let
339         (top_decls, mono_binds, sigs) = go ([], EmptyMonoBinds, []) bind 
340     in
341     (ValD (MonoBind mono_binds sigs Recursive) : top_decls)
342   where
343     go acc                RdrNullBind            = acc
344     go acc                (RdrAndBindings b1 b2) = go (go acc b1) b2
345     go (topds, mbs, sigs) (RdrHsDecl d)          = (d : topds, mbs, sigs)
346     go (topds, mbs, sigs) (RdrSig (FixSig d))    = (FixD d  : topds, mbs, sigs)
347     go (topds, mbs, sigs) (RdrSig sig)           = (topds, mbs, sig:sigs)
348     go (topds, mbs, sigs) (RdrValBinding bind)   = (topds, mbs `AndMonoBinds` bind, sigs)
349 \end{code}