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.
13 -- NB: don't reexport HsCore
14 -- this module tells about "real Haskell"
26 collectHsBinders, collectHsOutBinders, collectLocatedHsBinders,
27 collectMonoBinders, collectLocatedMonoBinders,
28 collectSigTysFromMonoBinds,
29 hsModuleName, hsModuleImports
32 #include "HsVersions.h"
42 import BasicTypes ( Fixity, Version, NewOrData )
45 import Name ( NamedThing )
47 import SrcLoc ( SrcLoc )
48 import Module ( ModuleName )
51 All we actually declare here is the top-level structure for a module.
53 data HsModule name pat
55 ModuleName -- module name
56 (Maybe Version) -- source interface version number
57 (Maybe [IE name]) -- export list; Nothing => export everything
58 -- Just [] => export *nothing* (???)
59 -- Just [...] => as you would expect...
60 [ImportDecl name] -- We snaffle interesting stuff out of the
61 -- imported interfaces early on, adding that
62 -- info to TyDecls/etc; so this list is
63 -- often empty, downstream.
64 [HsDecl name pat] -- Type, class, value, and interface signature decls
65 (Maybe DeprecTxt) -- reason/explanation for deprecation of this module
70 instance (NamedThing name, Outputable name, Outputable pat)
71 => Outputable (HsModule name pat) where
73 ppr (HsModule name iface_version exports imports
77 Nothing -> pp_header (ptext SLIT("where"))
80 nest 8 (fsep (punctuate comma (map ppr es))),
81 nest 4 (ptext SLIT(") where"))
87 pp_header rest = case deprec of
88 Nothing -> pp_modname <+> rest
89 Just d -> vcat [ pp_modname, ppr d, rest ]
91 pp_modname = ptext SLIT("module") <+> ppr name
94 pp_nonnull xs = vcat (map ppr xs)
96 hsModuleName (HsModule mod_name _ _ _ _ _ _) = mod_name
97 hsModuleImports (HsModule mod_name vers exports imports decls deprec src_loc) = imports
101 %************************************************************************
103 \subsection{Collecting binders from @HsBinds@}
105 %************************************************************************
107 Get all the binders in some @MonoBinds@, IN THE ORDER OF APPEARANCE.
109 These functions are here, rather than in HsBinds, to avoid a loop between HsPat and HsBinds.
118 it should return @[x, y, f, a, b]@ (remember, order important).
121 collectLocatedHsBinders :: HsBinds name (InPat name) -> [(name,SrcLoc)]
122 collectLocatedHsBinders EmptyBinds = []
123 collectLocatedHsBinders (MonoBind b _ _)
124 = collectLocatedMonoBinders b
125 collectLocatedHsBinders (ThenBinds b1 b2)
126 = collectLocatedHsBinders b1 ++ collectLocatedHsBinders b2
128 collectHsBinders :: HsBinds name (InPat name) -> [name]
129 collectHsBinders EmptyBinds = []
130 collectHsBinders (MonoBind b _ _)
131 = collectMonoBinders b
132 collectHsBinders (ThenBinds b1 b2)
133 = collectHsBinders b1 ++ collectHsBinders b2
135 -- corresponds to `collectHsBinders', but operates on renamed patterns
137 collectHsOutBinders :: HsBinds name (OutPat name) -> [name]
138 collectHsOutBinders EmptyBinds = []
139 collectHsOutBinders (MonoBind b _ _)
140 = collectMonoOutBinders b
141 collectHsOutBinders (ThenBinds b1 b2)
142 = collectHsOutBinders b1 ++ collectHsOutBinders b2
144 collectLocatedMonoBinders :: MonoBinds name (InPat name) -> [(name,SrcLoc)]
145 collectLocatedMonoBinders binds
148 go EmptyMonoBinds acc = acc
149 go (PatMonoBind pat _ loc) acc = map (\v->(v,loc)) (collectPatBinders pat) ++ acc
150 go (FunMonoBind f _ _ loc) acc = (f,loc) : acc
151 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
153 collectMonoBinders :: MonoBinds name (InPat name) -> [name]
154 collectMonoBinders binds
157 go EmptyMonoBinds acc = acc
158 go (PatMonoBind pat _ loc) acc = collectPatBinders pat ++ acc
159 go (FunMonoBind f _ _ loc) acc = f : acc
160 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
162 -- corresponds to `collectMonoBinders', but operates on renamed patterns
164 collectMonoOutBinders :: MonoBinds name (OutPat name) -> [name]
165 collectMonoOutBinders binds
168 go EmptyMonoBinds acc = acc
169 go (PatMonoBind pat _ loc) acc = collectOutPatBinders pat ++ acc
170 go (FunMonoBind f _ _ loc) acc = f : acc
171 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
174 %************************************************************************
176 \subsection{Getting patterns out of bindings}
178 %************************************************************************
180 Get all the pattern type signatures out of a bunch of bindings
183 collectSigTysFromMonoBinds :: MonoBinds name (InPat name) -> [HsType name]
184 collectSigTysFromMonoBinds bind
187 go EmptyMonoBinds acc = acc
188 go (PatMonoBind pat _ loc) acc = collectSigTysFromPat pat ++ acc
189 go (FunMonoBind f _ ms loc) acc = go_matches ms acc
190 go (AndMonoBinds bs1 bs2) acc = go bs1 (go bs2 acc)
192 -- A binding like x :: a = f y
193 -- is parsed as FunMonoBind, but for this purpose we
194 -- want to treat it as a pattern binding
195 go_matches [] acc = acc
196 go_matches (Match [] (Just sig) _ : matches) acc = sig : go_matches matches acc
197 go_matches (match : matches) acc = go_matches matches acc