2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section{Haskell abstract syntax definition}
6 This module glues together the pieces of the Haskell abstract syntax,
7 which is declared in the various \tr{Hs*} modules. This module,
8 therefore, is almost nothing but re-exporting.
21 HsModule(..), HsExtCore(..),
22 collectStmtsBinders, collectStmtBinders,
23 collectHsBinders, collectLocatedHsBinders,
24 collectMonoBinders, collectLocatedMonoBinders,
25 collectSigTysFromHsBinds, collectSigTysFromMonoBinds
28 #include "HsVersions.h"
38 import HscTypes ( DeprecTxt )
39 import BasicTypes ( Fixity, NewOrData )
42 import IfaceSyn ( IfaceBinding )
44 import SrcLoc ( SrcLoc )
45 import Module ( Module )
48 All we actually declare here is the top-level structure for a module.
52 (Maybe Module) -- Nothing => "module X where" is omitted
53 -- (in which case the next field is Nothing too)
54 (Maybe [IE name]) -- Export list; Nothing => export list omitted, so export everything
55 -- Just [] => export *nothing*
56 -- Just [...] => as you would expect...
57 [ImportDecl name] -- We snaffle interesting stuff out of the
58 -- imported interfaces early on, adding that
59 -- info to TyDecls/etc; so this list is
60 -- often empty, downstream.
61 [HsDecl name] -- Type, class, value, and interface signature decls
62 (Maybe DeprecTxt) -- reason/explanation for deprecation of this module
65 data HsExtCore name -- Read from Foo.hcr
68 [TyClDecl name] -- Type declarations only; just as in Haskell source,
69 -- so that we can infer kinds etc
70 [IfaceBinding] -- And the bindings
74 instance (OutputableBndr name)
75 => Outputable (HsModule name) where
77 ppr (HsModule Nothing _ imports decls _ src_loc)
78 = pp_nonnull imports $$ pp_nonnull decls
80 ppr (HsModule (Just name) exports imports decls deprec src_loc)
83 Nothing -> pp_header (ptext SLIT("where"))
86 nest 8 (fsep (punctuate comma (map ppr es))),
87 nest 4 (ptext SLIT(") where"))
93 pp_header rest = case deprec of
94 Nothing -> pp_modname <+> rest
95 Just d -> vcat [ pp_modname, ppr d, rest ]
97 pp_modname = ptext SLIT("module") <+> ppr name
100 pp_nonnull xs = vcat (map ppr xs)
104 %************************************************************************
106 \subsection{Collecting binders from @HsBinds@}
108 %************************************************************************
110 Get all the binders in some @MonoBinds@, IN THE ORDER OF APPEARANCE.
112 These functions are here, rather than in HsBinds, to avoid a loop between HsPat and HsBinds.
121 it should return @[x, y, f, a, b]@ (remember, order important).
124 collectLocatedHsBinders :: HsBinds name -> [(name,SrcLoc)]
125 -- Used at top level only; so no need for an IPBinds case
126 collectLocatedHsBinders EmptyBinds = []
127 collectLocatedHsBinders (MonoBind b _ _)
128 = collectLocatedMonoBinders b
129 collectLocatedHsBinders (ThenBinds b1 b2)
130 = collectLocatedHsBinders b1 ++ collectLocatedHsBinders b2
132 collectHsBinders :: HsBinds name -> [name]
133 collectHsBinders EmptyBinds = []
134 collectHsBinders (IPBinds _) = [] -- Implicit parameters don't create
136 collectHsBinders (MonoBind b _ _) = collectMonoBinders b
137 collectHsBinders (ThenBinds b1 b2) = collectHsBinders b1 ++ collectHsBinders b2
139 collectLocatedMonoBinders :: MonoBinds name -> [(name,SrcLoc)]
140 collectLocatedMonoBinders binds
143 go EmptyMonoBinds acc = acc
144 go (PatMonoBind pat _ loc) acc = map (\v->(v,loc)) (collectPatBinders pat) ++ acc
145 go (FunMonoBind f _ _ loc) acc = (f,loc) : acc
146 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
148 collectMonoBinders :: MonoBinds name -> [name]
149 collectMonoBinders binds
152 go EmptyMonoBinds acc = acc
153 go (PatMonoBind pat _ loc) acc = collectPatBinders pat ++ acc
154 go (FunMonoBind f _ _ loc) acc = f : acc
155 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
156 go (VarMonoBind v _) acc = v : acc
157 go (AbsBinds _ _ dbinds _ binds) acc
158 = [dp | (_,dp,_) <- dbinds] ++ go binds acc
162 %************************************************************************
164 \subsection{Getting patterns out of bindings}
166 %************************************************************************
168 Get all the pattern type signatures out of a bunch of bindings
171 collectSigTysFromHsBinds :: HsBinds name -> [HsType name]
172 collectSigTysFromHsBinds EmptyBinds = []
173 collectSigTysFromHsBinds (IPBinds _) = []
174 collectSigTysFromHsBinds (MonoBind b _ _) = collectSigTysFromMonoBinds b
175 collectSigTysFromHsBinds (ThenBinds b1 b2) = collectSigTysFromHsBinds b1 ++
176 collectSigTysFromHsBinds b2
179 collectSigTysFromMonoBinds :: MonoBinds name -> [HsType name]
180 collectSigTysFromMonoBinds bind
183 go EmptyMonoBinds acc = acc
184 go (PatMonoBind pat _ loc) acc = collectSigTysFromPat pat ++ acc
185 go (FunMonoBind f _ ms loc) acc = go_matches ms acc
186 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
188 -- A binding like x :: a = f y
189 -- is parsed as FunMonoBind, but for this purpose we
190 -- want to treat it as a pattern binding
191 go_matches [] acc = acc
192 go_matches (Match [] (Just sig) _ : matches) acc = sig : go_matches matches acc
193 go_matches (match : matches) acc = go_matches matches acc
197 collectStmtsBinders :: [Stmt id] -> [id]
198 collectStmtsBinders = concatMap collectStmtBinders
200 collectStmtBinders :: Stmt id -> [id]
201 -- Id Binders for a Stmt... [but what about pattern-sig type vars]?
202 collectStmtBinders (BindStmt pat _ _) = collectPatBinders pat
203 collectStmtBinders (LetStmt binds) = collectHsBinders binds
204 collectStmtBinders (ExprStmt _ _ _) = []
205 collectStmtBinders (ResultStmt _ _) = []
206 collectStmtBinders (RecStmt ss _ _ _) = collectStmtsBinders ss
207 collectStmtBinders other = panic "collectStmtBinders"