ea103620c7359e0a2e686cc2c79f0104c971acee
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsSyn.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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(..), HsIdInfo,  SpecDataSig(..), 
39                           hsDeclName
40                         )
41 import HsExpr
42 import HsImpExp
43 import HsBasic
44 import HsMatches
45 import HsPat
46 import HsTypes
47 import HsPragmas        ( ClassPragmas, ClassOpPragmas,
48                           DataPragmas, GenPragmas, InstancePragmas )
49 import HsCore
50 import BasicTypes       ( Fixity, Version, NewOrData, IfaceFlavour, Module )
51
52 -- others:
53 import FiniteMap        ( FiniteMap )
54 import Outputable
55 import SrcLoc           ( SrcLoc )
56 import Bag
57 import Name             ( NamedThing )
58 \end{code}
59
60 All we actually declare here is the top-level structure for a module.
61 \begin{code}
62 data HsModule flexi name pat
63   = HsModule
64         Module                  -- module name
65         (Maybe Version)         -- source interface version number
66         (Maybe [IE name])       -- export list; Nothing => export everything
67                                 -- Just [] => export *nothing* (???)
68                                 -- Just [...] => as you would expect...
69         [ImportDecl name]       -- We snaffle interesting stuff out of the
70                                 -- imported interfaces early on, adding that
71                                 -- info to TyDecls/etc; so this list is
72                                 -- often empty, downstream.
73         [FixityDecl name]
74         [HsDecl flexi name pat] -- Type, class, value, and interface signature decls
75         SrcLoc
76 \end{code}
77
78 \begin{code}
79 instance (NamedThing name, Outputable name, Outputable pat)
80         => Outputable (HsModule flexi name pat) where
81
82     ppr (HsModule name iface_version exports imports fixities
83                       decls src_loc)
84       = vcat [
85             case exports of
86               Nothing -> hsep [ptext SLIT("module"), ptext name, ptext SLIT("where")]
87               Just es -> vcat [
88                             hsep [ptext SLIT("module"), ptext name, lparen],
89                             nest 8 (interpp'SP es),
90                             nest 4 (ptext SLIT(") where"))
91                           ],
92             pp_nonnull imports,
93             pp_nonnull fixities,
94             pp_nonnull decls
95         ]
96       where
97         pp_nonnull [] = empty
98         pp_nonnull xs = vcat (map ppr xs)
99
100         pp_iface_version Nothing  = empty
101         pp_iface_version (Just n) = hsep [text "{-# INTERFACE", int n, text "#-}"]
102 \end{code}
103
104
105 %************************************************************************
106 %*                                                                      *
107 \subsection{Collecting binders from @HsBinds@}
108 %*                                                                      *
109 %************************************************************************
110
111 Get all the binders in some @MonoBinds@, IN THE ORDER OF APPEARANCE.
112
113 These functions are here, rather than in HsBinds, to avoid a loop between HsPat and HsBinds.
114
115 \begin{verbatim}
116 ...
117 where
118   (x, y) = ...
119   f i j  = ...
120   [a, b] = ...
121 \end{verbatim}
122 it should return @[x, y, f, a, b]@ (remember, order important).
123
124 \begin{code}
125 collectTopBinders :: HsBinds flexi name (InPat name) -> Bag (name,SrcLoc)
126 collectTopBinders EmptyBinds     = emptyBag
127 collectTopBinders (MonoBind b _ _) = collectMonoBinders b
128 collectTopBinders (ThenBinds b1 b2)
129  = collectTopBinders b1 `unionBags` collectTopBinders b2
130
131 collectMonoBinders :: MonoBinds flexi name (InPat name) -> Bag (name,SrcLoc)
132 collectMonoBinders EmptyMonoBinds                      = emptyBag
133 collectMonoBinders (PatMonoBind pat grhss_w_binds loc) = listToBag (map (\v->(v,loc)) (collectPatBinders pat))
134 collectMonoBinders (FunMonoBind f _ matches loc)       = unitBag (f,loc)
135 collectMonoBinders (VarMonoBind v expr)                = error "collectMonoBinders"
136 collectMonoBinders (CoreMonoBind v expr)               = error "collectMonoBinders"
137 collectMonoBinders (AndMonoBinds bs1 bs2)
138  = collectMonoBinders bs1 `unionBags` collectMonoBinders bs2
139 \end{code}
140