[project @ 1997-09-24 09:08:21 by simonm]
[ghc-hetmet.git] / ghc / compiler / reader / PrefixToHs.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrefixToHS]{Support routines for converting ``prefix form'' to Haskell abstract syntax}
5
6 Support routines for reading prefix-form from the Lex/Yacc parser.
7
8 \begin{code}
9 #include "HsVersions.h"
10
11 module PrefixToHs (
12         cvValSig,
13         cvClassOpSig,
14         cvInstDeclSig,
15
16         cvBinds,
17         cvMonoBindsAndSigs,
18         cvMatches,
19         cvOtherDecls
20     ) where
21
22 IMP_Ubiq(){-uitous-}
23
24 import PrefixSyn        -- and various syntaxen.
25 import HsSyn
26 import RdrHsSyn
27 import HsPragmas        ( noGenPragmas, noClassOpPragmas )
28
29 import SrcLoc           ( mkSrcLoc )
30 import Util             ( mapAndUnzip, panic, assertPanic )
31 \end{code}
32
33 %************************************************************************
34 %*                                                                      *
35 \subsection[cvDecls]{Convert various top-level declarations}
36 %*                                                                      *
37 %************************************************************************
38
39 We make a point not to throw any user-pragma ``sigs'' at
40 these conversion functions:
41 \begin{code}
42 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
43
44 cvValSig (RdrTySig vars poly_ty src_loc)
45   = [ Sig v poly_ty src_loc | v <- vars ]
46
47 cvClassOpSig (RdrTySig vars poly_ty src_loc)
48   = [ ClassOpSig v Nothing poly_ty src_loc | v <- vars ]
49
50 cvInstDeclSig (RdrSpecValSig        sigs) = sigs
51 cvInstDeclSig (RdrInlineValSig      sig)  = [ sig ]
52 cvInstDeclSig (RdrMagicUnfoldingSig sig)  = [ sig ]
53 \end{code}
54
55 %************************************************************************
56 %*                                                                      *
57 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
58 %*                                                                      *
59 %************************************************************************
60
61 Function definitions are restructured here. Each is assumed to be recursive
62 initially, and non recursive definitions are discovered by the dependency
63 analyser.
64
65 \begin{code}
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
70     }
71 \end{code}
72
73 \begin{code}
74 cvMonoBindsAndSigs :: SrcFile
75                    -> SigConverter
76                    -> RdrBinding
77                    -> (RdrNameMonoBinds, [RdrNameSig])
78
79 cvMonoBindsAndSigs sf sig_cvtr fb
80   = mangle_bind (EmptyMonoBinds, []) fb
81   where
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.
89
90     mangle_bind acc (RdrAndBindings fb1 fb2)
91       = mangle_bind (mangle_bind acc fb1) fb2
92
93     mangle_bind (b_acc, s_acc) sig@(RdrTySig _ _ _)
94       = (b_acc, s_acc ++ sig_cvtr sig)
95
96     mangle_bind (b_acc, s_acc) (RdrSpecValSig        sig) = (b_acc, sig ++ s_acc)
97     mangle_bind (b_acc, s_acc) (RdrInlineValSig      sig) = (b_acc, sig : s_acc)
98     mangle_bind (b_acc, s_acc) (RdrMagicUnfoldingSig sig) = (b_acc, sig : s_acc)
99
100     mangle_bind (b_acc, s_acc)
101                 (RdrPatternBinding lousy_srcline [patbinding])
102       -- WDP: the parser has trouble getting a good line-number on RdrPatternBindings.
103       = case (cvPatMonoBind sf patbinding) of { (pat, grhss, binds) ->
104         let
105             src_loc = mkSrcLoc sf good_srcline
106         in
107         (b_acc `AndMonoBinds`
108          PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
109         }
110       where
111         good_srcline = case patbinding of
112                          RdrMatch_NoGuard ln _ _ _ _ -> ln
113                          RdrMatch_Guards  ln _ _ _ _ -> ln
114
115
116     mangle_bind _ (RdrPatternBinding _ _)
117       = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
118
119     mangle_bind (b_acc, s_acc) (RdrFunctionBinding srcline patbindings)
120             -- must be a function binding...
121       = case (cvFunMonoBind sf patbindings) of { (var, inf, matches) ->
122         (b_acc `AndMonoBinds`
123          FunMonoBind var inf matches (mkSrcLoc sf srcline), s_acc)
124         }
125
126     mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
127 \end{code}
128
129 \begin{code}
130 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
131
132 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
133   = (pat, [OtherwiseGRHS expr (mkSrcLoc sf srcline)], cvBinds sf cvValSig binding)
134
135 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
136   = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
137
138 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
139
140 cvFunMonoBind sf matches
141   = (head srcfuns, head infixdefs, cvMatches sf False matches)
142   where
143     (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
144     -- ToDo: Check for consistent srcfun and infixdef
145
146     get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
147     get_mdef (RdrMatch_Guards  _ sfun pat _ _) = get_pdef pat
148
149     get_pdef (ConPatIn fn _)       = (fn, False)
150     get_pdef (ConOpPatIn _ op _ _) = (op, True)
151     get_pdef (ParPatIn pat)        = get_pdef pat
152
153
154 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
155 cvMatch   :: SrcFile -> Bool -> RdrMatch   -> RdrNameMatch
156
157 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
158
159 cvMatch sf is_case rdr_match
160   = foldr PatMatch
161           (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
162
163           -- For a FunMonoBinds, the first flattened "pattern" is
164           -- just the function name, and we don't want to keep it.
165           -- For a case expr, it's (presumably) a constructor name -- and
166           -- we most certainly want to keep it!  Hence the monkey busines...
167
168           (if is_case then -- just one pattern: leave it untouched...
169               [pat]
170            else            -- function pattern; extract arg patterns...
171               case pat of ConPatIn fn pats      -> pats
172                           ConOpPatIn p1 op _ p2 -> [p1,p2]
173                           ParPatIn pat          -> panic "PrefixToHs.cvMatch:ParPatIn"
174           )
175   where
176     (pat, binding, guarded_exprs)
177       = case rdr_match of
178           RdrMatch_NoGuard ln b c expr    d -> (c,d, [OtherwiseGRHS expr (mkSrcLoc sf ln)])
179           RdrMatch_Guards  ln b c gd_exps d -> (c,d, map (cvGRHS sf ln) gd_exps)
180
181 cvGRHS :: SrcFile -> SrcLine -> ([RdrNameStmt], RdrNameHsExpr) -> RdrNameGRHS
182 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc sf sl)
183 \end{code}
184
185 %************************************************************************
186 %*                                                                      *
187 \subsection[PrefixToHS-utils]{Utilities for conversion}
188 %*                                                                      *
189 %************************************************************************
190
191 Separate declarations into all the various kinds:
192
193 \begin{code}
194 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
195 cvOtherDecls b 
196   = go [] b
197   where
198     go acc (RdrAndBindings b1 b2) = go (go acc b1) b2
199     go acc (RdrTyDecl d)          = TyD d   : acc
200     go acc (RdrClassDecl d)       = ClD d   : acc
201     go acc (RdrInstDecl d)        = InstD d : acc 
202     go acc (RdrDefaultDecl d)     = DefD d  : acc
203     go acc other                  = acc
204 \end{code}