2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrefixToHS]{Support routines for converting ``prefix form'' to Haskell abstract syntax}
6 Support routines for reading prefix-form from the Lex/Yacc parser.
9 #include "HsVersions.h"
20 sepDeclsIntoSigsAndBinds
25 import PrefixSyn -- and various syntaxen.
28 import HsPragmas ( noGenPragmas, noClassOpPragmas )
30 import SrcLoc ( mkSrcLoc2 )
31 import Util ( panic, assertPanic )
34 %************************************************************************
36 \subsection[cvDecls]{Convert various top-level declarations}
38 %************************************************************************
40 We make a point not to throw any user-pragma ``sigs'' at
41 these conversion functions:
43 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
45 cvValSig (RdrTySig vars poly_ty src_loc)
46 = [ Sig v poly_ty noGenPragmas src_loc | v <- vars ]
48 cvClassOpSig (RdrTySig vars poly_ty src_loc)
49 = [ ClassOpSig v poly_ty noClassOpPragmas src_loc | v <- vars ]
51 cvInstDeclSig (RdrSpecValSig sigs) = sigs
52 cvInstDeclSig (RdrInlineValSig sig) = [ sig ]
53 cvInstDeclSig (RdrDeforestSig sig) = [ sig ]
54 cvInstDeclSig (RdrMagicUnfoldingSig sig) = [ sig ]
57 %************************************************************************
59 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
61 %************************************************************************
63 Function definitions are restructured here. Each is assumed to be recursive
64 initially, and non recursive definitions are discovered by the dependency
68 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
69 cvBinds sf sig_cvtr raw_binding
70 = cvSepdBinds sf sig_cvtr (sepDeclsForBinds raw_binding)
72 cvSepdBinds :: SrcFile -> SigConverter -> [RdrBinding] -> RdrNameHsBinds
73 cvSepdBinds sf sig_cvtr bindings
74 = case (mkMonoBindsAndSigs sf sig_cvtr bindings) of { (mbs, sigs) ->
76 then SingleBind (RecBind mbs)
77 else BindWith (RecBind mbs) sigs
80 cvMonoBinds :: SrcFile -> [RdrBinding] -> RdrNameMonoBinds
81 cvMonoBinds sf bindings
82 = case (mkMonoBindsAndSigs sf bottom bindings) of { (mbs,sigs) ->
85 else panic "cvMonoBinds: some sigs present"
88 bottom = panic "cvMonoBinds: sig converter!"
92 mkMonoBindsAndSigs :: SrcFile
95 -> (RdrNameMonoBinds, [RdrNameSig])
97 mkMonoBindsAndSigs sf sig_cvtr fbs
98 = foldl mangle_bind (EmptyMonoBinds, []) fbs
100 -- If the function being bound has at least one argument, then the
101 -- guarded right hand sides of each pattern binding are knitted
102 -- into a series of patterns, each matched with its corresponding
103 -- guarded right hand side (which may contain several
104 -- alternatives). This series is then paired with the name of the
105 -- function. Otherwise there is only one pattern, which is paired
106 -- with a guarded right hand side.
108 mangle_bind (b_acc, s_acc) sig@(RdrTySig _ _ _)
109 = (b_acc, s_acc ++ sig_cvtr sig)
111 mangle_bind (b_acc, s_acc) (RdrSpecValSig sig) = (b_acc, sig ++ s_acc)
112 mangle_bind (b_acc, s_acc) (RdrInlineValSig sig) = (b_acc, sig : s_acc)
113 mangle_bind (b_acc, s_acc) (RdrDeforestSig sig) = (b_acc, sig : s_acc)
114 mangle_bind (b_acc, s_acc) (RdrMagicUnfoldingSig sig) = (b_acc, sig : s_acc)
116 mangle_bind (b_acc, s_acc)
117 (RdrPatternBinding lousy_srcline [patbinding])
118 -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
119 = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
121 src_loc = mkSrcLoc2 sf good_srcline
123 (b_acc `AndMonoBinds`
124 PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
127 good_srcline = case patbinding of
128 RdrMatch_NoGuard ln _ _ _ _ -> ln
129 RdrMatch_Guards ln _ _ _ _ -> ln
132 mangle_bind _ (RdrPatternBinding _ _)
133 = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
135 mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
136 -- must be a function binding...
137 = case (cvFunMonoBind sf patbindings) of { (var, matches) ->
138 (b_acc `AndMonoBinds`
139 FunMonoBind var matches (mkSrcLoc2 sf srcline), s_acc)
144 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
146 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
147 = (pat, [OtherwiseGRHS expr (mkSrcLoc2 sf srcline)], cvBinds sf cvValSig binding)
149 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
150 = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
152 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, [RdrNameMatch])
154 cvFunMonoBind sf matches
155 = (srcfun {- cheating ... -}, cvMatches sf False matches)
157 srcfun = case (head matches) of
158 RdrMatch_NoGuard _ sfun _ _ _ -> sfun
159 RdrMatch_Guards _ sfun _ _ _ -> sfun
161 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
162 cvMatch :: SrcFile -> Bool -> RdrMatch -> RdrNameMatch
164 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
166 cvMatch sf is_case rdr_match
168 (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
170 -- For a FunMonoBinds, the first flattened "pattern" is
171 -- just the function name, and we don't want to keep it.
172 -- For a case expr, it's (presumably) a constructor name -- and
173 -- we most certainly want to keep it! Hence the monkey busines...
175 (if is_case then -- just one pattern: leave it untouched...
179 ConPatIn _ pats -> pats
182 (pat, binding, guarded_exprs)
184 RdrMatch_NoGuard ln b c expr d -> (c,d, [OtherwiseGRHS expr (mkSrcLoc2 sf ln)])
185 RdrMatch_Guards ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
187 ---------------------
188 pat' = doctor_pat pat
190 -- a ConOpPatIn in the corner may be handled by converting it to
193 doctor_pat (ConOpPatIn p1 op p2) = ConPatIn op [p1, p2]
194 doctor_pat other_pat = other_pat
196 cvGRHS :: SrcFile -> SrcLine -> (RdrNameHsExpr, RdrNameHsExpr) -> RdrNameGRHS
198 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc2 sf sl)
201 %************************************************************************
203 \subsection[PrefixToHS-utils]{Utilities for conversion}
205 %************************************************************************
207 Separate declarations into all the various kinds:
210 ty "sigs" RdrSpecDataSig
213 inst "sigs" RdrSpecInstSig
214 defaults RdrDefaultDecl
215 binds RdrFunctionBinding RdrPatternBinding RdrTySig
216 RdrSpecValSig RdrInlineValSig RdrDeforestSig
220 This function isn't called directly; some other function calls it,
221 then checks that what it got is appropriate for that situation.
222 (Those functions follow...)
225 sepDecls (RdrTyDecl a)
226 tys tysigs classes insts instsigs defaults binds
227 = (a:tys,tysigs,classes,insts,instsigs,defaults,binds)
229 sepDecls a@(RdrFunctionBinding _ _)
230 tys tysigs classes insts instsigs defaults binds
231 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
233 sepDecls a@(RdrPatternBinding _ _)
234 tys tysigs classes insts instsigs defaults binds
235 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
237 -- RdrAndBindings catered for below...
239 sepDecls (RdrClassDecl a)
240 tys tysigs classes insts instsigs defaults binds
241 = (tys,tysigs,a:classes,insts,instsigs,defaults,binds)
243 sepDecls (RdrInstDecl a)
244 tys tysigs classes insts instsigs defaults binds
245 = (tys,tysigs,classes,a:insts,instsigs,defaults,binds)
247 sepDecls (RdrDefaultDecl a)
248 tys tysigs classes insts instsigs defaults binds
249 = (tys,tysigs,classes,insts,instsigs,a:defaults,binds)
251 sepDecls a@(RdrTySig _ _ _)
252 tys tysigs classes insts instsigs defaults binds
253 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
255 sepDecls a@(RdrSpecValSig _)
256 tys tysigs classes insts instsigs defaults binds
257 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
259 sepDecls a@(RdrInlineValSig _)
260 tys tysigs classes insts instsigs defaults binds
261 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
263 sepDecls a@(RdrDeforestSig _)
264 tys tysigs classes insts instsigs defaults binds
265 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
267 sepDecls a@(RdrMagicUnfoldingSig _)
268 tys tysigs classes insts instsigs defaults binds
269 = (tys,tysigs,classes,insts,instsigs,defaults,a:binds)
271 sepDecls (RdrSpecInstSig a)
272 tys tysigs classes insts instsigs defaults binds
273 = (tys,tysigs,classes,insts,a:instsigs,defaults,binds)
275 sepDecls (RdrSpecDataSig a)
276 tys tysigs classes insts instsigs defaults binds
277 = (tys,a:tysigs,classes,insts,instsigs,defaults,binds)
280 tys tysigs classes insts instsigs defaults binds
281 = (tys,tysigs,classes,insts,instsigs,defaults,binds)
283 sepDecls (RdrAndBindings bs1 bs2)
284 tys tysigs classes insts instsigs defaults binds
285 = case (sepDecls bs2 tys tysigs classes insts instsigs defaults binds) of {
286 (tys,tysigs,classes,insts,instsigs,defaults,binds) ->
287 sepDecls bs1 tys tysigs classes insts instsigs defaults binds
292 sepDeclsForTopBinds binding
293 = sepDecls binding [] [] [] [] [] [] []
295 sepDeclsForBinds binding
296 = case (sepDecls binding [] [] [] [] [] [] [])
297 of { (tys,tysigs,classes,insts,instsigs,defaults,binds) ->
307 sepDeclsIntoSigsAndBinds binding
308 = case (sepDeclsForBinds binding) of { sigs_and_binds ->
309 foldr sep_stuff ([],[]) sigs_and_binds
312 sep_stuff s@(RdrTySig _ _ _) (sigs,defs) = (s:sigs,defs)
313 sep_stuff s@(RdrSpecValSig _) (sigs,defs) = (s:sigs,defs)
314 sep_stuff s@(RdrInlineValSig _) (sigs,defs) = (s:sigs,defs)
315 sep_stuff s@(RdrDeforestSig _) (sigs,defs) = (s:sigs,defs)
316 sep_stuff s@(RdrMagicUnfoldingSig _) (sigs,defs) = (s:sigs,defs)
317 sep_stuff d@(RdrFunctionBinding _ _) (sigs,defs) = (sigs,d:defs)
318 sep_stuff d@(RdrPatternBinding _ _) (sigs,defs) = (sigs,d:defs)