72525b2519b7dd36fa4f745784167591fa1b1140
[ghc.git] / compiler / hsSyn / HsSyn.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section{Haskell abstract syntax definition}
6
7 This module glues together the pieces of the Haskell abstract syntax,
8 which is declared in the various \tr{Hs*} modules. This module,
9 therefore, is almost nothing but re-exporting.
10 -}
11
12 {-# LANGUAGE DeriveDataTypeable #-}
13 {-# LANGUAGE StandaloneDeriving #-}
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
16 -- in module PlaceHolder
17 {-# LANGUAGE ConstraintKinds #-}
18
19 module HsSyn (
20 module HsBinds,
21 module HsDecls,
22 module HsExpr,
23 module HsImpExp,
24 module HsLit,
25 module HsPat,
26 module HsTypes,
27 module HsUtils,
28 module HsDoc,
29 module PlaceHolder,
30 Fixity,
31
32 HsModule(..)
33 ) where
34
35 -- friends:
36 import HsDecls
37 import HsBinds
38 import HsExpr
39 import HsImpExp
40 import HsLit
41 import PlaceHolder
42 import HsPat
43 import HsTypes
44 import BasicTypes ( Fixity, WarningTxt )
45 import HsUtils
46 import HsDoc
47
48 -- others:
49 import OccName ( HasOccName )
50 import Outputable
51 import SrcLoc
52 import Module ( ModuleName )
53 import FastString
54
55 -- libraries:
56 import Data.Data hiding ( Fixity )
57
58 -- | All we actually declare here is the top-level structure for a module.
59 data HsModule name
60 = HsModule {
61 hsmodName :: Maybe (Located ModuleName),
62 -- ^ @Nothing@: \"module X where\" is omitted (in which case the next
63 -- field is Nothing too)
64 hsmodExports :: Maybe (Located [LIE name]),
65 -- ^ Export list
66 --
67 -- - @Nothing@: export list omitted, so export everything
68 --
69 -- - @Just []@: export /nothing/
70 --
71 -- - @Just [...]@: as you would expect...
72 --
73 --
74 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
75 -- ,'ApiAnnotation.AnnClose'
76
77 -- For details on above see note [Api annotations] in ApiAnnotation
78 hsmodImports :: [LImportDecl name],
79 -- ^ We snaffle interesting stuff out of the imported interfaces early
80 -- on, adding that info to TyDecls/etc; so this list is often empty,
81 -- downstream.
82 hsmodDecls :: [LHsDecl name],
83 -- ^ Type, class, value, and interface signature decls
84 hsmodDeprecMessage :: Maybe (Located WarningTxt),
85 -- ^ reason\/explanation for warning/deprecation of this module
86 --
87 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
88 -- ,'ApiAnnotation.AnnClose'
89 --
90
91 -- For details on above see note [Api annotations] in ApiAnnotation
92 hsmodHaddockModHeader :: Maybe LHsDocString
93 -- ^ Haddock module info and description, unparsed
94 --
95 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
96 -- ,'ApiAnnotation.AnnClose'
97
98 -- For details on above see note [Api annotations] in ApiAnnotation
99 }
100 -- ^ 'ApiAnnotation.AnnKeywordId's
101 --
102 -- - 'ApiAnnotation.AnnModule','ApiAnnotation.AnnWhere'
103 --
104 -- - 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnSemi',
105 -- 'ApiAnnotation.AnnClose' for explicit braces and semi around
106 -- hsmodImports,hsmodDecls if this style is used.
107
108 -- For details on above see note [Api annotations] in ApiAnnotation
109 deriving (Typeable)
110 deriving instance (DataId name) => Data (HsModule name)
111
112 instance (OutputableBndr name, HasOccName name)
113 => Outputable (HsModule name) where
114
115 ppr (HsModule Nothing _ imports decls _ mbDoc)
116 = pp_mb mbDoc $$ pp_nonnull imports
117 $$ pp_nonnull decls
118
119 ppr (HsModule (Just name) exports imports decls deprec mbDoc)
120 = vcat [
121 pp_mb mbDoc,
122 case exports of
123 Nothing -> pp_header (ptext (sLit "where"))
124 Just es -> vcat [
125 pp_header lparen,
126 nest 8 (fsep (punctuate comma (map ppr (unLoc es)))),
127 nest 4 (ptext (sLit ") where"))
128 ],
129 pp_nonnull imports,
130 pp_nonnull decls
131 ]
132 where
133 pp_header rest = case deprec of
134 Nothing -> pp_modname <+> rest
135 Just d -> vcat [ pp_modname, ppr d, rest ]
136
137 pp_modname = ptext (sLit "module") <+> ppr name
138
139 pp_mb :: Outputable t => Maybe t -> SDoc
140 pp_mb (Just x) = ppr x
141 pp_mb Nothing = empty
142
143 pp_nonnull :: Outputable t => [t] -> SDoc
144 pp_nonnull [] = empty
145 pp_nonnull xs = vcat (map ppr xs)