[project @ 1998-12-02 13:17:09 by simonm]
[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, IfaceFlavour,
26
27         collectTopBinders, collectMonoBinders
28      ) where
29
30 #include "HsVersions.h"
31
32 -- friends:
33 import HsBinds
34 import HsDecls          ( HsDecl(..), TyDecl(..), InstDecl(..), ClassDecl(..), 
35                           DefaultDecl(..), ForeignDecl(..), ForKind(..),
36                           ExtName(..), isDynamic,  FixityDecl(..), 
37                           ConDecl(..), ConDetails(..), BangType(..),
38                           IfaceSig(..),  SpecDataSig(..), 
39                           hsDeclName
40                         )
41 import HsExpr
42 import HsImpExp
43 import HsBasic
44 import HsMatches
45 import HsPat
46 import HsTypes
47 import HsCore
48 import BasicTypes       ( Fixity, Version, NewOrData, IfaceFlavour, Module )
49
50 -- others:
51 import Outputable
52 import SrcLoc           ( SrcLoc )
53 import Bag
54 import Name             ( NamedThing )
55 \end{code}
56
57 All we actually declare here is the top-level structure for a module.
58 \begin{code}
59 data HsModule flexi name pat
60   = HsModule
61         Module                  -- module name
62         (Maybe Version)         -- source interface version number
63         (Maybe [IE name])       -- export list; Nothing => export everything
64                                 -- Just [] => export *nothing* (???)
65                                 -- Just [...] => as you would expect...
66         [ImportDecl name]       -- We snaffle interesting stuff out of the
67                                 -- imported interfaces early on, adding that
68                                 -- info to TyDecls/etc; so this list is
69                                 -- often empty, downstream.
70         [FixityDecl name]
71         [HsDecl flexi name pat] -- Type, class, value, and interface signature decls
72         SrcLoc
73 \end{code}
74
75 \begin{code}
76 instance (NamedThing name, Outputable name, Outputable pat)
77         => Outputable (HsModule flexi name pat) where
78
79     ppr (HsModule name iface_version exports imports fixities
80                       decls src_loc)
81       = vcat [
82             case exports of
83               Nothing -> hsep [ptext SLIT("module"), ptext name, ptext SLIT("where")]
84               Just es -> vcat [
85                             hsep [ptext SLIT("module"), ptext name, lparen],
86                             nest 8 (interpp'SP es),
87                             nest 4 (ptext SLIT(") where"))
88                           ],
89             pp_nonnull imports,
90             pp_nonnull fixities,
91             pp_nonnull decls
92         ]
93       where
94         pp_nonnull [] = empty
95         pp_nonnull xs = vcat (map ppr xs)
96
97         pp_iface_version Nothing  = empty
98         pp_iface_version (Just n) = hsep [text "{-# INTERFACE", int n, text "#-}"]
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 flexi name (InPat name) -> Bag (name,SrcLoc)
123 collectTopBinders EmptyBinds     = emptyBag
124 collectTopBinders (MonoBind b _ _) = collectMonoBinders b
125 collectTopBinders (ThenBinds b1 b2)
126  = collectTopBinders b1 `unionBags` collectTopBinders b2
127
128 collectMonoBinders :: MonoBinds flexi name (InPat name) -> Bag (name,SrcLoc)
129 collectMonoBinders EmptyMonoBinds                      = emptyBag
130 collectMonoBinders (PatMonoBind pat grhss_w_binds loc) = listToBag (map (\v->(v,loc)) (collectPatBinders pat))
131 collectMonoBinders (FunMonoBind f _ matches loc)       = unitBag (f,loc)
132 collectMonoBinders (VarMonoBind v expr)                = error "collectMonoBinders"
133 collectMonoBinders (CoreMonoBind v expr)               = error "collectMonoBinders"
134 collectMonoBinders (AndMonoBinds bs1 bs2)
135  = collectMonoBinders bs1 `unionBags` collectMonoBinders bs2
136 \end{code}
137