Add Template Haskell support for overloaded labels
[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 module HsExtension,
31 Fixity,
32
33 HsModule(..)
34 ) where
35
36 -- friends:
37 import HsDecls
38 import HsBinds
39 import HsExpr
40 import HsImpExp
41 import HsLit
42 import PlaceHolder
43 import HsExtension
44 import HsPat
45 import HsTypes
46 import BasicTypes ( Fixity, WarningTxt )
47 import HsUtils
48 import HsDoc
49
50 -- others:
51 import Outputable
52 import SrcLoc
53 import Module ( ModuleName )
54
55 -- libraries:
56 import Data.Data hiding ( Fixity )
57
58 -- | Haskell Module
59 --
60 -- All we actually declare here is the top-level structure for a module.
61 data HsModule name
62 = HsModule {
63 hsmodName :: Maybe (Located ModuleName),
64 -- ^ @Nothing@: \"module X where\" is omitted (in which case the next
65 -- field is Nothing too)
66 hsmodExports :: Maybe (Located [LIE name]),
67 -- ^ Export list
68 --
69 -- - @Nothing@: export list omitted, so export everything
70 --
71 -- - @Just []@: export /nothing/
72 --
73 -- - @Just [...]@: as you would expect...
74 --
75 --
76 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
77 -- ,'ApiAnnotation.AnnClose'
78
79 -- For details on above see note [Api annotations] in ApiAnnotation
80 hsmodImports :: [LImportDecl name],
81 -- ^ We snaffle interesting stuff out of the imported interfaces early
82 -- on, adding that info to TyDecls/etc; so this list is often empty,
83 -- downstream.
84 hsmodDecls :: [LHsDecl name],
85 -- ^ Type, class, value, and interface signature decls
86 hsmodDeprecMessage :: Maybe (Located WarningTxt),
87 -- ^ reason\/explanation for warning/deprecation of this module
88 --
89 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
90 -- ,'ApiAnnotation.AnnClose'
91 --
92
93 -- For details on above see note [Api annotations] in ApiAnnotation
94 hsmodHaddockModHeader :: Maybe LHsDocString
95 -- ^ Haddock module info and description, unparsed
96 --
97 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen'
98 -- ,'ApiAnnotation.AnnClose'
99
100 -- For details on above see note [Api annotations] in ApiAnnotation
101 }
102 -- ^ 'ApiAnnotation.AnnKeywordId's
103 --
104 -- - 'ApiAnnotation.AnnModule','ApiAnnotation.AnnWhere'
105 --
106 -- - 'ApiAnnotation.AnnOpen','ApiAnnotation.AnnSemi',
107 -- 'ApiAnnotation.AnnClose' for explicit braces and semi around
108 -- hsmodImports,hsmodDecls if this style is used.
109
110 -- For details on above see note [Api annotations] in ApiAnnotation
111 deriving instance (DataId name) => Data (HsModule name)
112
113 instance (SourceTextX pass, OutputableBndrId pass)
114 => Outputable (HsModule pass) where
115
116 ppr (HsModule Nothing _ imports decls _ mbDoc)
117 = pp_mb mbDoc $$ pp_nonnull imports
118 $$ pp_nonnull decls
119
120 ppr (HsModule (Just name) exports imports decls deprec mbDoc)
121 = vcat [
122 pp_mb mbDoc,
123 case exports of
124 Nothing -> pp_header (text "where")
125 Just es -> vcat [
126 pp_header lparen,
127 nest 8 (fsep (punctuate comma (map ppr (unLoc es)))),
128 nest 4 (text ") where")
129 ],
130 pp_nonnull imports,
131 pp_nonnull decls
132 ]
133 where
134 pp_header rest = case deprec of
135 Nothing -> pp_modname <+> rest
136 Just d -> vcat [ pp_modname, ppr d, rest ]
137
138 pp_modname = text "module" <+> ppr name
139
140 pp_mb :: Outputable t => Maybe t -> SDoc
141 pp_mb (Just x) = ppr x
142 pp_mb Nothing = empty
143
144 pp_nonnull :: Outputable t => [t] -> SDoc
145 pp_nonnull [] = empty
146 pp_nonnull xs = vcat (map ppr xs)