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.
20 #include "HsVersions.h"
22 import PrefixSyn -- and various syntaxen.
25 import HsPragmas ( noGenPragmas, noClassOpPragmas )
27 import BasicTypes ( RecFlag(..) )
28 import SrcLoc ( mkSrcLoc )
29 import Util ( mapAndUnzip, panic, assertPanic )
32 %************************************************************************
34 \subsection[cvDecls]{Convert various top-level declarations}
36 %************************************************************************
38 We make a point not to throw any user-pragma ``sigs'' at
39 these conversion functions:
42 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
46 cvInstDeclSig sig = sig
48 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
49 cvClassOpSig sig = sig
53 %************************************************************************
55 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
57 %************************************************************************
59 Function definitions are restructured here. Each is assumed to be recursive
60 initially, and non recursive definitions are discovered by the dependency
64 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
65 cvBinds sf sig_cvtr binding
66 = case (cvMonoBindsAndSigs sf sig_cvtr binding) of { (mbs, sigs) ->
67 MonoBind mbs sigs Recursive
72 cvMonoBindsAndSigs :: SrcFile
75 -> (RdrNameMonoBinds, [RdrNameSig])
77 cvMonoBindsAndSigs sf sig_cvtr fb
78 = mangle_bind (EmptyMonoBinds, []) fb
80 -- If the function being bound has at least one argument, then the
81 -- guarded right hand sides of each pattern binding are knitted
82 -- into a series of patterns, each matched with its corresponding
83 -- guarded right hand side (which may contain several
84 -- alternatives). This series is then paired with the name of the
85 -- function. Otherwise there is only one pattern, which is paired
86 -- with a guarded right hand side.
88 mangle_bind acc (RdrAndBindings fb1 fb2)
89 = mangle_bind (mangle_bind acc fb1) fb2
91 mangle_bind (b_acc, s_acc) (RdrSig sig)
92 = (b_acc, sig_cvtr sig : s_acc)
94 mangle_bind (b_acc, s_acc)
95 (RdrPatternBinding lousy_srcline [patbinding])
96 -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
97 = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
99 src_loc = mkSrcLoc sf good_srcline
101 (b_acc `AndMonoBinds`
102 PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
105 good_srcline = case patbinding of
106 RdrMatch_NoGuard ln _ _ _ _ -> ln
107 RdrMatch_Guards ln _ _ _ _ -> ln
110 mangle_bind _ (RdrPatternBinding _ _)
111 = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
113 mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
114 -- must be a function binding...
115 = case (cvFunMonoBind sf patbindings) of { (var, inf, matches) ->
116 (b_acc `AndMonoBinds`
117 FunMonoBind var inf matches (mkSrcLoc sf srcline), s_acc)
120 mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
121 -- Ignore class decls, instance decls etc
125 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
127 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
128 = (pat, unguardedRHS expr (mkSrcLoc sf srcline), cvBinds sf cvValSig binding)
130 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
131 = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
133 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
135 cvFunMonoBind sf matches
136 = (head srcfuns, head infixdefs, cvMatches sf False matches)
138 (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
139 -- ToDo: Check for consistent srcfun and infixdef
141 get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
142 get_mdef (RdrMatch_Guards _ sfun pat _ _) = get_pdef pat
144 get_pdef (ConPatIn fn _) = (fn, False)
145 get_pdef (ConOpPatIn _ op _ _) = (op, True)
146 get_pdef (ParPatIn pat) = get_pdef pat
149 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
150 cvMatch :: SrcFile -> Bool -> RdrMatch -> RdrNameMatch
152 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
154 cvMatch sf is_case rdr_match
156 (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
158 -- For a FunMonoBinds, the first flattened "pattern" is
159 -- just the function name, and we don't want to keep it.
160 -- For a case expr, it's (presumably) a constructor name -- and
161 -- we most certainly want to keep it! Hence the monkey busines...
163 (if is_case then -- just one pattern: leave it untouched...
165 else -- function pattern; extract arg patterns...
166 case pat of ConPatIn fn pats -> pats
167 ConOpPatIn p1 op _ p2 -> [p1,p2]
168 ParPatIn pat -> panic "PrefixToHs.cvMatch:ParPatIn"
171 (pat, binding, guarded_exprs)
173 RdrMatch_NoGuard ln b c expr d -> (c,d, unguardedRHS expr (mkSrcLoc sf ln))
174 RdrMatch_Guards ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
176 cvGRHS :: SrcFile -> SrcLine -> ([RdrNameStmt], RdrNameHsExpr) -> RdrNameGRHS
177 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc sf sl)
180 %************************************************************************
182 \subsection[PrefixToHS-utils]{Utilities for conversion}
184 %************************************************************************
186 Separate declarations into all the various kinds:
189 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
193 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
194 go acc (RdrTyDecl d) = TyD d : acc
195 go acc (RdrClassDecl d) = ClD d : acc
196 go acc (RdrInstDecl d) = InstD d : acc
197 go acc (RdrDefaultDecl d) = DefD d : acc
199 -- Ignore value bindings