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.
13 cvValSig, cvClassOpSig, cvInstDeclSig
16 #include "HsVersions.h"
18 import PrefixSyn -- and various syntaxen.
22 import BasicTypes ( RecFlag(..) )
23 import SrcLoc ( mkSrcLoc )
24 import Util ( mapAndUnzip )
25 import Panic ( panic, assertPanic )
28 %************************************************************************
30 \subsection[cvDecls]{Convert various top-level declarations}
32 %************************************************************************
34 We make a point not to throw any user-pragma ``sigs'' at
35 these conversion functions:
38 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
42 cvInstDeclSig sig = sig
44 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
45 cvClassOpSig sig = sig
49 %************************************************************************
51 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
53 %************************************************************************
55 Function definitions are restructured here. Each is assumed to be recursive
56 initially, and non recursive definitions are discovered by the dependency
60 cvBinds :: SrcFile -> SigConverter -> RdrBinding -> RdrNameHsBinds
61 -- The mysterious SigConverter converts Sigs to ClassOpSigs
62 -- in class declarations. Mostly it's just an identity function
64 cvBinds sf sig_cvtr binding
65 = case (cvMonoBindsAndSigs sf sig_cvtr binding) of { (mbs, sigs) ->
66 MonoBind mbs sigs Recursive
71 cvMonoBindsAndSigs :: SrcFile
74 -> (RdrNameMonoBinds, [RdrNameSig])
76 cvMonoBindsAndSigs sf sig_cvtr fb
77 = mangle_bind (EmptyMonoBinds, []) fb
79 mangle_bind acc RdrNullBind
82 mangle_bind acc (RdrAndBindings fb1 fb2)
83 = mangle_bind (mangle_bind acc fb1) fb2
85 mangle_bind (b_acc, s_acc) (RdrSig sig)
86 = (b_acc, sig_cvtr sig : s_acc)
88 mangle_bind (b_acc, s_acc) (RdrValBinding binding)
89 = (b_acc `AndMonoBinds` binding, s_acc)
93 %************************************************************************
95 \subsection[PrefixToHS-utils]{Utilities for conversion}
97 %************************************************************************
99 Separate declarations into all the various kinds:
102 cvTopDecls :: SrcFile -> RdrBinding -> [RdrNameHsDecl]
103 cvTopDecls srcfile bind
105 (top_decls, mono_binds, sigs) = go ([], EmptyMonoBinds, []) bind
107 (ValD (MonoBind mono_binds sigs Recursive) : top_decls)
109 go acc RdrNullBind = acc
110 go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
111 go (topds, mbs, sigs) (RdrHsDecl d) = (d : topds, mbs, sigs)
112 go (topds, mbs, sigs) (RdrSig (FixSig d)) = (FixD d : topds, mbs, sigs)
113 go (topds, mbs, sigs) (RdrSig sig) = (topds, mbs, sig:sigs)
114 go (topds, mbs, sigs) (RdrValBinding bind) = (topds, mbs `AndMonoBinds` bind, sigs)