[project @ 2000-02-15 22:18:16 by panne]
[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 or HsPragmas;
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 HsBasic,
22         module HsMatches,
23         module HsPat,
24         module HsTypes,
25         Fixity, NewOrData, 
26
27         collectTopBinders, collectMonoBinders
28      ) where
29
30 #include "HsVersions.h"
31
32 -- friends:
33 import HsDecls          
34 import HsBinds
35 import HsExpr
36 import HsImpExp
37 import HsBasic
38 import HsMatches
39 import HsPat
40 import HsTypes
41 import HsCore
42 import BasicTypes       ( Fixity, Version, NewOrData )
43
44 -- others:
45 import Outputable
46 import SrcLoc           ( SrcLoc )
47 import Bag
48 import Module           ( ModuleName, pprModuleName )
49 \end{code}
50
51 All we actually declare here is the top-level structure for a module.
52 \begin{code}
53 data HsModule name pat
54   = HsModule
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
66         SrcLoc
67 \end{code}
68
69 \begin{code}
70 instance (Outputable name, Outputable pat)
71         => Outputable (HsModule name pat) where
72
73     ppr (HsModule name iface_version exports imports
74                       decls deprec src_loc)
75       = vcat [
76             case deprec of
77               Nothing -> empty
78               Just dt -> hsep [ptext SLIT("{-# DEPRECATED"), ppr dt, ptext SLIT("#-}")],
79             case exports of
80               Nothing -> hsep [ptext SLIT("module"), pprModuleName name, ptext SLIT("where")]
81               Just es -> vcat [
82                             hsep [ptext SLIT("module"), pprModuleName name, lparen],
83                             nest 8 (fsep (punctuate comma (map ppr es))),
84                             nest 4 (ptext SLIT(") where"))
85                           ],
86             pp_nonnull imports,
87             pp_nonnull decls
88         ]
89       where
90         pp_nonnull [] = empty
91         pp_nonnull xs = vcat (map ppr xs)
92
93         pp_iface_version Nothing  = empty
94         pp_iface_version (Just n) = hsep [text "{-# INTERFACE", int n, text "#-}"]
95 \end{code}
96
97
98 %************************************************************************
99 %*                                                                      *
100 \subsection{Collecting binders from @HsBinds@}
101 %*                                                                      *
102 %************************************************************************
103
104 Get all the binders in some @MonoBinds@, IN THE ORDER OF APPEARANCE.
105
106 These functions are here, rather than in HsBinds, to avoid a loop between HsPat and HsBinds.
107
108 \begin{verbatim}
109 ...
110 where
111   (x, y) = ...
112   f i j  = ...
113   [a, b] = ...
114 \end{verbatim}
115 it should return @[x, y, f, a, b]@ (remember, order important).
116
117 \begin{code}
118 collectTopBinders :: HsBinds name (InPat name) -> Bag (name,SrcLoc)
119 collectTopBinders EmptyBinds     = emptyBag
120 collectTopBinders (MonoBind b _ _) = collectMonoBinders b
121 collectTopBinders (ThenBinds b1 b2)
122  = collectTopBinders b1 `unionBags` collectTopBinders b2
123
124 collectMonoBinders :: MonoBinds name (InPat name) -> Bag (name,SrcLoc)
125 collectMonoBinders EmptyMonoBinds                = emptyBag
126 collectMonoBinders (PatMonoBind pat _ loc)       = listToBag (map (\v->(v,loc)) (collectPatBinders pat))
127 collectMonoBinders (FunMonoBind f _ matches loc) = unitBag (f,loc)
128 collectMonoBinders (VarMonoBind v expr)          = error "collectMonoBinders"
129 collectMonoBinders (CoreMonoBind v expr)         = error "collectMonoBinders"
130 collectMonoBinders (AndMonoBinds bs1 bs2)        = collectMonoBinders bs1 `unionBags`
131                                                    collectMonoBinders bs2
132 \end{code}
133