[project @ 1998-04-06 18:38:36 by sof]
[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 module PrefixToHs (
10         cvValSig,
11         cvClassOpSig,
12         cvInstDeclSig,
13
14         cvBinds,
15         cvMonoBindsAndSigs,
16         cvMatches,
17         cvOtherDecls
18     ) where
19
20 #include "HsVersions.h"
21
22 import PrefixSyn        -- and various syntaxen.
23 import HsSyn
24 import RdrHsSyn
25 import HsPragmas        ( noGenPragmas, noClassOpPragmas )
26
27 import BasicTypes       ( RecFlag(..) )
28 import SrcLoc           ( mkSrcLoc )
29 import Util             ( mapAndUnzip, panic, assertPanic )
30 \end{code}
31
32 %************************************************************************
33 %*                                                                      *
34 \subsection[cvDecls]{Convert various top-level declarations}
35 %*                                                                      *
36 %************************************************************************
37
38 We make a point not to throw any user-pragma ``sigs'' at
39 these conversion functions:
40
41 \begin{code}
42 cvValSig, cvClassOpSig, cvInstDeclSig :: SigConverter
43
44 cvValSig      sig = sig
45
46 cvInstDeclSig sig = sig
47
48 cvClassOpSig (Sig var poly_ty src_loc) = ClassOpSig var Nothing poly_ty src_loc
49 cvClassOpSig sig                       = sig
50 \end{code}
51
52
53 %************************************************************************
54 %*                                                                      *
55 \subsection[cvBinds-etc]{Converting to @HsBinds@, @MonoBinds@, etc.}
56 %*                                                                      *
57 %************************************************************************
58
59 Function definitions are restructured here. Each is assumed to be recursive
60 initially, and non recursive definitions are discovered by the dependency
61 analyser.
62
63 \begin{code}
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
68     }
69 \end{code}
70
71 \begin{code}
72 cvMonoBindsAndSigs :: SrcFile
73                    -> SigConverter
74                    -> RdrBinding
75                    -> (RdrNameMonoBinds, [RdrNameSig])
76
77 cvMonoBindsAndSigs sf sig_cvtr fb
78   = mangle_bind (EmptyMonoBinds, []) fb
79   where
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.
87
88     mangle_bind acc (RdrAndBindings fb1 fb2)
89       = mangle_bind (mangle_bind acc fb1) fb2
90
91     mangle_bind (b_acc, s_acc) (RdrSig sig)
92       = (b_acc, sig_cvtr sig : s_acc)
93
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) ->
98         let
99             src_loc = mkSrcLoc sf good_srcline
100         in
101         (b_acc `AndMonoBinds`
102          PatMonoBind pat (GRHSsAndBindsIn grhss binds) src_loc, s_acc)
103         }
104       where
105         good_srcline = case patbinding of
106                          RdrMatch_NoGuard ln _ _ _ _ -> ln
107                          RdrMatch_Guards  ln _ _ _ _ -> ln
108
109
110     mangle_bind _ (RdrPatternBinding _ _)
111       = panic "mangleBinding: more than one pattern on a RdrPatternBinding"
112
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)
118         }
119
120     mangle_bind (b_acc, s_acc) other = (b_acc, s_acc)
121         -- Ignore class decls, instance decls etc
122 \end{code}
123
124 \begin{code}
125 cvPatMonoBind :: SrcFile -> RdrMatch -> (RdrNamePat, [RdrNameGRHS], RdrNameHsBinds)
126
127 cvPatMonoBind sf (RdrMatch_NoGuard srcline srcfun pat expr binding)
128   = (pat, unguardedRHS expr (mkSrcLoc sf srcline), cvBinds sf cvValSig binding)
129
130 cvPatMonoBind sf (RdrMatch_Guards srcline srcfun pat guardedexprs binding)
131   = (pat, map (cvGRHS sf srcline) guardedexprs, cvBinds sf cvValSig binding)
132
133 cvFunMonoBind :: SrcFile -> [RdrMatch] -> (RdrName {-VarName-}, Bool {-InfixDefn-}, [RdrNameMatch])
134
135 cvFunMonoBind sf matches
136   = (head srcfuns, head infixdefs, cvMatches sf False matches)
137   where
138     (srcfuns, infixdefs) = mapAndUnzip get_mdef matches
139     -- ToDo: Check for consistent srcfun and infixdef
140
141     get_mdef (RdrMatch_NoGuard _ sfun pat _ _) = get_pdef pat
142     get_mdef (RdrMatch_Guards  _ sfun pat _ _) = get_pdef pat
143
144     get_pdef (ConPatIn fn _)       = (fn, False)
145     get_pdef (ConOpPatIn _ op _ _) = (op, True)
146     get_pdef (ParPatIn pat)        = get_pdef pat
147
148
149 cvMatches :: SrcFile -> Bool -> [RdrMatch] -> [RdrNameMatch]
150 cvMatch   :: SrcFile -> Bool -> RdrMatch   -> RdrNameMatch
151
152 cvMatches sf is_case matches = map (cvMatch sf is_case) matches
153
154 cvMatch sf is_case rdr_match
155   = foldr PatMatch
156           (GRHSMatch (GRHSsAndBindsIn guarded_exprs (cvBinds sf cvValSig binding)))
157
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...
162
163           (if is_case then -- just one pattern: leave it untouched...
164               [pat]
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"
169           )
170   where
171     (pat, binding, guarded_exprs)
172       = case rdr_match of
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)
175
176 cvGRHS :: SrcFile -> SrcLine -> ([RdrNameStmt], RdrNameHsExpr) -> RdrNameGRHS
177 cvGRHS sf sl (g, e) = GRHS g e (mkSrcLoc sf sl)
178 \end{code}
179
180 %************************************************************************
181 %*                                                                      *
182 \subsection[PrefixToHS-utils]{Utilities for conversion}
183 %*                                                                      *
184 %************************************************************************
185
186 Separate declarations into all the various kinds:
187
188 \begin{code}
189 cvOtherDecls :: RdrBinding -> [RdrNameHsDecl]
190 cvOtherDecls b 
191   = go [] b
192   where
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
198     go acc other                  = acc
199         -- Ignore value bindings
200 \end{code}