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.
18 cvForeignDecls -- HACK
22 #include "HsVersions.h"
24 import PrefixSyn -- and various syntaxen.
27 import HsPragmas ( noGenPragmas, noClassOpPragmas )
29 import BasicTypes ( RecFlag(..) )
30 import SrcLoc ( mkSrcLoc )
31 import Util ( mapAndUnzip, 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:
44 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
48 cvInstDeclSig sig = sig
50 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
51 cvClassOpSig sig = sig
55 %************************************************************************
57 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
59 %************************************************************************
61 Function definitions are restructured here. Each is assumed to be recursive
62 initially, and non recursive definitions are discovered by the dependency
66 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
67 cvBinds sf sig_cvtr binding
68 = case (cvMonoBindsAndSigs sf sig_cvtr binding) of { (mbs, sigs) ->
69 MonoBind mbs sigs Recursive
74 cvMonoBindsAndSigs :: SrcFile
77 -> (RdrNameMonoBinds, [RdrNameSig])
79 cvMonoBindsAndSigs sf sig_cvtr fb
80 = mangle_bind (EmptyMonoBinds, []) fb
82 -- If the function being bound has at least one argument, then the
83 -- guarded right hand sides of each pattern binding are knitted
84 -- into a series of patterns, each matched with its corresponding
85 -- guarded right hand side (which may contain several
86 -- alternatives). This series is then paired with the name of the
87 -- function. Otherwise there is only one pattern, which is paired
88 -- with a guarded right hand side.
90 mangle_bind acc (RdrAndBindings fb1 fb2)
91 = mangle_bind (mangle_bind acc fb1) fb2
93 mangle_bind (b_acc, s_acc) (RdrSig sig)
94 = (b_acc, sig_cvtr sig : s_acc)
96 mangle_bind (b_acc, s_acc)
97 (RdrPatternBinding lousy_srcline [patbinding])
98 -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
99 = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
101 src_loc = mkSrcLoc sf good_srcline
103 (b_acc `AndMonoBinds`
104 PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
107 good_srcline = case patbinding of
108 RdrMatch_NoGuard ln _ _ _ _ -> ln
109 RdrMatch_Guards ln _ _ _ _ -> ln
112 mangle_bind _ (RdrPatternBinding _ _)
113 = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
115 mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
116 -- must be a function binding...
117 = case (cvFunMonoBind sf patbindings) of { (var, inf, matches) ->
118 (b_acc `AndMonoBinds`
119 FunMonoBind var inf matches (mkSrcLoc sf srcline), s_acc)
122 mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
123 -- Ignore class decls, instance decls etc
127 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
129 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
130 = (pat, unguardedRHS expr (mkSrcLoc sf srcline), cvBinds sf cvValSig binding)
132 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
133 = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
135 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
137 cvFunMonoBind sf matches
138 = (head srcfuns, head infixdefs, cvMatches sf False matches)
140 (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
141 -- ToDo: Check for consistent srcfun and infixdef
143 get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
144 get_mdef (RdrMatch_Guards _ sfun pat _ _) = get_pdef pat
146 get_pdef (ConPatIn fn _) = (fn, False)
147 get_pdef (ConOpPatIn _ op _ _) = (op, True)
148 get_pdef (ParPatIn pat) = get_pdef pat
151 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
152 cvMatch :: SrcFile -> Bool -> RdrMatch -> RdrNameMatch
154 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
156 cvMatch sf is_case rdr_match
158 (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
160 -- For a FunMonoBinds, the first flattened "pattern" is
161 -- just the function name, and we don't want to keep it.
162 -- For a case expr, it's (presumably) a constructor name -- and
163 -- we most certainly want to keep it! Hence the monkey busines...
165 (if is_case then -- just one pattern: leave it untouched...
167 else -- function pattern; extract arg patterns...
168 case pat of ConPatIn fn pats -> pats
169 ConOpPatIn p1 op _ p2 -> [p1,p2]
170 ParPatIn pat -> panic "PrefixToHs.cvMatch:ParPatIn"
173 (pat, binding, guarded_exprs)
175 RdrMatch_NoGuard ln b c expr d -> (c,d, unguardedRHS expr (mkSrcLoc sf ln))
176 RdrMatch_Guards ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
178 cvGRHS :: SrcFile -> SrcLine -> ([RdrNameStmt], RdrNameHsExpr) -> RdrNameGRHS
179 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc sf sl)
182 %************************************************************************
184 \subsection[PrefixToHS-utils]{Utilities for conversion}
186 %************************************************************************
188 Separate declarations into all the various kinds:
191 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
195 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
196 go acc (RdrTyDecl d) = TyD d : acc
197 go acc (RdrClassDecl d) = ClD d : acc
198 go acc (RdrInstDecl d) = InstD d : acc
199 go acc (RdrDefaultDecl d) = DefD d : acc
201 -- Ignore value bindings
203 cvForeignDecls :: RdrBinding -> [RdrNameHsDecl]
204 cvForeignDecls b = go [] b
206 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
207 go acc (RdrForeignDecl d) = ForD d : acc