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"
24 import PrefixSyn -- and various syntaxen.
27 import HsPragmas ( noGenPragmas, noClassOpPragmas )
29 import SrcLoc ( mkSrcLoc )
30 import Util ( mapAndUnzip, panic, assertPanic )
33 %************************************************************************
35 \subsection[cvDecls]{Convert various top-level declarations}
37 %************************************************************************
39 We make a point not to throw any user-pragma ``sigs'' at
40 these conversion functions:
42 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
44 cvValSig (RdrTySig vars poly_ty src_loc)
45 = [ Sig v poly_ty src_loc | v <- vars ]
47 cvClassOpSig (RdrTySig vars poly_ty src_loc)
48 = [ ClassOpSig v poly_ty noClassOpPragmas src_loc | v <- vars ]
50 cvInstDeclSig (RdrSpecValSig sigs) = sigs
51 cvInstDeclSig (RdrInlineValSig sig) = [ sig ]
52 cvInstDeclSig (RdrDeforestSig sig) = [ sig ]
53 cvInstDeclSig (RdrMagicUnfoldingSig sig) = [ sig ]
56 %************************************************************************
58 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
60 %************************************************************************
62 Function definitions are restructured here. Each is assumed to be recursive
63 initially, and non recursive definitions are discovered by the dependency
67 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
68 cvBinds sf sig_cvtr binding
69 = case (cvMonoBindsAndSigs sf sig_cvtr binding) of { (mbs, sigs) ->
71 then SingleBind (RecBind mbs)
72 else BindWith (RecBind mbs) sigs
77 cvMonoBindsAndSigs :: SrcFile
80 -> (RdrNameMonoBinds, [RdrNameSig])
82 cvMonoBindsAndSigs sf sig_cvtr fb
83 = mangle_bind (EmptyMonoBinds, []) fb
85 -- If the function being bound has at least one argument, then the
86 -- guarded right hand sides of each pattern binding are knitted
87 -- into a series of patterns, each matched with its corresponding
88 -- guarded right hand side (which may contain several
89 -- alternatives). This series is then paired with the name of the
90 -- function. Otherwise there is only one pattern, which is paired
91 -- with a guarded right hand side.
93 mangle_bind acc (RdrAndBindings fb1 fb2)
94 = mangle_bind (mangle_bind acc fb1) fb2
96 mangle_bind (b_acc, s_acc) sig@(RdrTySig _ _ _)
97 = (b_acc, s_acc ++ sig_cvtr sig)
99 mangle_bind (b_acc, s_acc) (RdrSpecValSig sig) = (b_acc, sig ++ s_acc)
100 mangle_bind (b_acc, s_acc) (RdrInlineValSig sig) = (b_acc, sig : s_acc)
101 mangle_bind (b_acc, s_acc) (RdrDeforestSig sig) = (b_acc, sig : s_acc)
102 mangle_bind (b_acc, s_acc) (RdrMagicUnfoldingSig sig) = (b_acc, sig : s_acc)
104 mangle_bind (b_acc, s_acc)
105 (RdrPatternBinding lousy_srcline [patbinding])
106 -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
107 = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
109 src_loc = mkSrcLoc sf good_srcline
111 (b_acc `AndMonoBinds`
112 PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
115 good_srcline = case patbinding of
116 RdrMatch_NoGuard ln _ _ _ _ -> ln
117 RdrMatch_Guards ln _ _ _ _ -> ln
120 mangle_bind _ (RdrPatternBinding _ _)
121 = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
123 mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
124 -- must be a function binding...
125 = case (cvFunMonoBind sf patbindings) of { (var, inf, matches) ->
126 (b_acc `AndMonoBinds`
127 FunMonoBind var inf matches (mkSrcLoc sf srcline), s_acc)
130 mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
134 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
136 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
137 = (pat, [OtherwiseGRHS expr (mkSrcLoc sf srcline)], cvBinds sf cvValSig binding)
139 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
140 = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
142 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
144 cvFunMonoBind sf matches
145 = (head srcfuns, head infixdefs, cvMatches sf False matches)
147 (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
148 -- ToDo: Check for consistent srcfun and infixdef
150 get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
151 get_mdef (RdrMatch_Guards _ sfun pat _ _) = get_pdef pat
153 get_pdef (ConPatIn fn _) = (fn, False)
154 get_pdef (ConOpPatIn _ op _ _) = (op, True)
155 get_pdef (ParPatIn pat) = get_pdef pat
158 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
159 cvMatch :: SrcFile -> Bool -> RdrMatch -> RdrNameMatch
161 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
163 cvMatch sf is_case rdr_match
165 (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
167 -- For a FunMonoBinds, the first flattened "pattern" is
168 -- just the function name, and we don't want to keep it.
169 -- For a case expr, it's (presumably) a constructor name -- and
170 -- we most certainly want to keep it! Hence the monkey busines...
172 (if is_case then -- just one pattern: leave it untouched...
174 else -- function pattern; extract arg patterns...
175 case pat of ConPatIn fn pats -> pats
176 ConOpPatIn p1 op _ p2 -> [p1,p2]
177 ParPatIn pat -> panic "PrefixToHs.cvMatch:ParPatIn"
180 (pat, binding, guarded_exprs)
182 RdrMatch_NoGuard ln b c expr d -> (c,d, [OtherwiseGRHS expr (mkSrcLoc sf ln)])
183 RdrMatch_Guards ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
185 cvGRHS :: SrcFile -> SrcLine -> (RdrNameHsExpr, RdrNameHsExpr) -> RdrNameGRHS
186 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc sf sl)
189 %************************************************************************
191 \subsection[PrefixToHS-utils]{Utilities for conversion}
193 %************************************************************************
195 Separate declarations into all the various kinds:
198 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
202 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
203 go acc (RdrTyDecl d) = TyD d : acc
204 go acc (RdrClassDecl d) = ClD d : acc
205 go acc (RdrInstDecl d) = InstD d : acc
206 go acc (RdrDefaultDecl d) = DefD d : acc