2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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.
28 import BasicTypes ( RecFlag(..) )
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:
43 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
47 cvInstDeclSig sig = sig
49 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
50 cvClassOpSig sig = sig
54 %************************************************************************
56 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
58 %************************************************************************
60 Function definitions are restructured here. Each is assumed to be recursive
61 initially, and non recursive definitions are discovered by the dependency
65 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
66 cvBinds sf sig_cvtr binding
67 = case (cvMonoBindsAndSigs sf sig_cvtr binding) of { (mbs, sigs) ->
68 MonoBind mbs sigs Recursive
73 cvMonoBindsAndSigs :: SrcFile
76 -> (RdrNameMonoBinds, [RdrNameSig])
78 cvMonoBindsAndSigs sf sig_cvtr fb
79 = mangle_bind (EmptyMonoBinds, []) fb
81 -- If the function being bound has at least one argument, then the
82 -- guarded right hand sides of each pattern binding are knitted
83 -- into a series of patterns, each matched with its corresponding
84 -- guarded right hand side (which may contain several
85 -- alternatives). This series is then paired with the name of the
86 -- function. Otherwise there is only one pattern, which is paired
87 -- with a guarded right hand side.
89 mangle_bind acc (RdrAndBindings fb1 fb2)
90 = mangle_bind (mangle_bind acc fb1) fb2
92 mangle_bind (b_acc, s_acc) (RdrSig sig)
93 = (b_acc, sig_cvtr sig : s_acc)
95 mangle_bind (b_acc, s_acc)
96 (RdrPatternBinding lousy_srcline [patbinding])
97 -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
98 = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
100 src_loc = mkSrcLoc sf good_srcline
102 (b_acc `AndMonoBinds`
103 PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
106 good_srcline = case patbinding of
107 RdrMatch_NoGuard ln _ _ _ _ -> ln
108 RdrMatch_Guards ln _ _ _ _ -> ln
111 mangle_bind _ (RdrPatternBinding _ _)
112 = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
114 mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
115 -- must be a function binding...
116 = case (cvFunMonoBind sf patbindings) of { (var, inf, matches) ->
117 (b_acc `AndMonoBinds`
118 FunMonoBind var inf matches (mkSrcLoc sf srcline), s_acc)
121 mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
122 -- Ignore class decls, instance decls etc
126 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
128 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
129 = (pat, unguardedRHS expr (mkSrcLoc sf srcline), cvBinds sf cvValSig binding)
131 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
132 = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
134 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
136 cvFunMonoBind sf matches
137 = (head srcfuns, head infixdefs, cvMatches sf False matches)
139 (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
140 -- ToDo: Check for consistent srcfun and infixdef
142 get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
143 get_mdef (RdrMatch_Guards _ sfun pat _ _) = get_pdef pat
145 get_pdef (ConPatIn fn _) = (fn, False)
146 get_pdef (ConOpPatIn _ op _ _) = (op, True)
147 get_pdef (ParPatIn pat) = get_pdef pat
150 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
151 cvMatch :: SrcFile -> Bool -> RdrMatch -> RdrNameMatch
153 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
155 cvMatch sf is_case rdr_match
157 (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
159 -- For a FunMonoBinds, the first flattened "pattern" is
160 -- just the function name, and we don't want to keep it.
161 -- For a case expr, it's (presumably) a constructor name -- and
162 -- we most certainly want to keep it! Hence the monkey busines...
164 (if is_case then -- just one pattern: leave it untouched...
166 else -- function pattern; extract arg patterns...
167 case pat of ConPatIn fn pats -> pats
168 ConOpPatIn p1 op _ p2 -> [p1,p2]
169 ParPatIn pat -> panic "PrefixToHs.cvMatch:ParPatIn"
172 (pat, binding, guarded_exprs)
174 RdrMatch_NoGuard ln b c expr d -> (c,d, unguardedRHS expr (mkSrcLoc sf ln))
175 RdrMatch_Guards ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
177 cvGRHS :: SrcFile -> SrcLine -> ([RdrNameStmt], RdrNameHsExpr) -> RdrNameGRHS
178 cvGRHS sf sl (g, e) = GRHS (g ++ [ExprStmt e locn]) locn
180 locn = mkSrcLoc sf sl
183 %************************************************************************
185 \subsection[PrefixToHS-utils]{Utilities for conversion}
187 %************************************************************************
189 Separate declarations into all the various kinds:
192 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
196 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
197 go acc (RdrTyDecl d) = TyD d : acc
198 go acc (RdrClassDecl d) = ClD d : acc
199 go acc (RdrInstDecl d) = InstD d : acc
200 go acc (RdrDefaultDecl d) = DefD d : acc
202 -- Ignore value bindings
204 cvForeignDecls :: RdrBinding -> [RdrNameHsDecl]
205 cvForeignDecls b = go [] b
207 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
208 go acc (RdrForeignDecl d) = ForD d : acc