compiler: de-lhs hsSyn/
[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 hsmodImports :: [LImportDecl name],
78 -- ^ We snaffle interesting stuff out of the imported interfaces early
79 -- on, adding that info to TyDecls/etc; so this list is often empty,
80 -- downstream.
81 hsmodDecls :: [LHsDecl name],
82 -- ^ Type, class, value, and interface signature decls
83 hsmodDeprecMessage :: Maybe (Located WarningTxt),
84 -- ^ reason\/explanation for warning/deprecation of this module
85 --
86 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
87 -- ,'ApiAnnotation.AnnClose'
88 --
89 hsmodHaddockModHeader :: Maybe LHsDocString
90 -- ^ Haddock module info and description, unparsed
91 --
92 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
93 -- ,'ApiAnnotation.AnnClose'
94 --
95 }
96 -- ^ 'ApiAnnotation.AnnKeywordId's
97 --
98 -- - 'ApiAnnotation.AnnModule','ApiAnnotation.AnnWhere'
99 --
100 -- - 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnSemi',
101 -- 'ApiAnnotation.AnnClose' for explicit braces and semi around
102 -- hsmodImports,hsmodDecls if this style is used.
103 --
104 deriving (Typeable)
105 deriving instance (DataId name) => Data (HsModule name)
106
107 instance (OutputableBndr name, HasOccName name)
108 => Outputable (HsModule name) where
109
110 ppr (HsModule Nothing _ imports decls _ mbDoc)
111 = pp_mb mbDoc $$ pp_nonnull imports
112 $$ pp_nonnull decls
113
114 ppr (HsModule (Just name) exports imports decls deprec mbDoc)
115 = vcat [
116 pp_mb mbDoc,
117 case exports of
118 Nothing -> pp_header (ptext (sLit "where"))
119 Just es -> vcat [
120 pp_header lparen,
121 nest 8 (fsep (punctuate comma (map ppr (unLoc es)))),
122 nest 4 (ptext (sLit ") where"))
123 ],
124 pp_nonnull imports,
125 pp_nonnull decls
126 ]
127 where
128 pp_header rest = case deprec of
129 Nothing -> pp_modname <+> rest
130 Just d -> vcat [ pp_modname, ppr d, rest ]
131
132 pp_modname = ptext (sLit "module") <+> ppr name
133
134 pp_mb :: Outputable t => Maybe t -> SDoc
135 pp_mb (Just x) = ppr x
136 pp_mb Nothing = empty
137
138 pp_nonnull :: Outputable t => [t] -> SDoc
139 pp_nonnull [] = empty
140 pp_nonnull xs = vcat (map ppr xs)