[project @ 2000-11-07 15:21:38 by simonmar]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section{Haskell abstract syntax definition}
5
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.
9
10 \begin{code}
11 module HsSyn (
12
13         -- NB: don't reexport HsCore
14         -- this module tells about "real Haskell"
15
16         module HsSyn,
17         module HsBinds,
18         module HsDecls,
19         module HsExpr,
20         module HsImpExp,
21         module HsLit,
22         module HsMatches,
23         module HsPat,
24         module HsTypes,
25         Fixity, NewOrData, 
26
27         collectTopBinders, collectMonoBinders, collectLocatedMonoBinders,
28         hsModuleName, hsModuleImports
29      ) where
30
31 #include "HsVersions.h"
32
33 -- friends:
34 import HsDecls          
35 import HsBinds
36 import HsExpr
37 import HsImpExp
38 import HsLit
39 import HsMatches
40 import HsPat
41 import HsTypes
42 import BasicTypes       ( Fixity, Version, NewOrData )
43
44 -- others:
45 import Name             ( NamedThing )
46 import Outputable
47 import SrcLoc           ( SrcLoc )
48 import Bag
49 import Module           ( ModuleName )
50 \end{code}
51
52 All we actually declare here is the top-level structure for a module.
53 \begin{code}
54 data HsModule name pat
55   = HsModule
56         ModuleName              -- module name
57         (Maybe Version)         -- source interface version number
58         (Maybe [IE name])       -- export list; Nothing => export everything
59                                 -- Just [] => export *nothing* (???)
60                                 -- Just [...] => as you would expect...
61         [ImportDecl name]       -- We snaffle interesting stuff out of the
62                                 -- imported interfaces early on, adding that
63                                 -- info to TyDecls/etc; so this list is
64                                 -- often empty, downstream.
65         [HsDecl name pat]       -- Type, class, value, and interface signature decls
66         (Maybe DeprecTxt)       -- reason/explanation for deprecation of this module
67         SrcLoc
68 \end{code}
69
70 \begin{code}
71 instance (NamedThing name, Outputable name, Outputable pat)
72         => Outputable (HsModule name pat) where
73
74     ppr (HsModule name iface_version exports imports
75                       decls deprec src_loc)
76       = vcat [
77             case exports of
78               Nothing -> pp_header (ptext SLIT("where"))
79               Just es -> vcat [
80                             pp_header lparen,
81                             nest 8 (fsep (punctuate comma (map ppr es))),
82                             nest 4 (ptext SLIT(") where"))
83                           ],
84             pp_nonnull imports,
85             pp_nonnull decls
86         ]
87       where
88         pp_header rest = case deprec of
89            Nothing -> pp_modname <+> rest
90            Just d -> vcat [ pp_modname, ppr d, rest ]
91
92         pp_modname = ptext SLIT("module") <+> ppr name
93
94         pp_nonnull [] = empty
95         pp_nonnull xs = vcat (map ppr xs)
96
97 hsModuleName    (HsModule mod_name _ _ _ _ _ _) = mod_name
98 hsModuleImports (HsModule mod_name vers exports imports decls deprec src_loc) = imports
99 \end{code}
100
101
102 %************************************************************************
103 %*                                                                      *
104 \subsection{Collecting binders from @HsBinds@}
105 %*                                                                      *
106 %************************************************************************
107
108 Get all the binders in some @MonoBinds@, IN THE ORDER OF APPEARANCE.
109
110 These functions are here, rather than in HsBinds, to avoid a loop between HsPat and HsBinds.
111
112 \begin{verbatim}
113 ...
114 where
115   (x, y) = ...
116   f i j  = ...
117   [a, b] = ...
118 \end{verbatim}
119 it should return @[x, y, f, a, b]@ (remember, order important).
120
121 \begin{code}
122 collectTopBinders :: HsBinds name (InPat name) -> Bag (name,SrcLoc)
123 collectTopBinders EmptyBinds        = emptyBag
124 collectTopBinders (MonoBind b _ _)  = listToBag (collectLocatedMonoBinders b)
125 collectTopBinders (ThenBinds b1 b2) = collectTopBinders b1 `unionBags` collectTopBinders b2
126
127 collectLocatedMonoBinders :: MonoBinds name (InPat name) -> [(name,SrcLoc)]
128 collectLocatedMonoBinders binds
129   = go binds []
130   where
131     go EmptyMonoBinds          acc = acc
132     go (PatMonoBind pat _ loc) acc = map (\v->(v,loc)) (collectPatBinders pat) ++ acc
133     go (FunMonoBind f _ _ loc) acc = (f,loc) : acc
134     go (AndMonoBinds bs1 bs2)  acc = go bs1 (go bs2 acc)
135
136 collectMonoBinders :: MonoBinds name (InPat name) -> [name]
137 collectMonoBinders binds
138   = go binds []
139   where
140     go EmptyMonoBinds          acc = acc
141     go (PatMonoBind pat _ loc) acc = collectPatBinders pat ++ acc
142     go (FunMonoBind f _ _ loc) acc = f : acc
143     go (AndMonoBinds bs1 bs2)  acc = go bs1 (go bs2 acc)
144 \end{code}