compiler: de-lhs hsSyn/
[ghc.git] / compiler / hsSyn / HsImpExp.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
6 HsImpExp: Abstract syntax: imports, exports, interfaces
7 -}
8
9 {-# LANGUAGE DeriveDataTypeable #-}
10
11 module HsImpExp where
12
13 import Module ( ModuleName )
14 import HsDoc ( HsDocString )
15 import OccName ( HasOccName(..), isTcOcc, isSymOcc )
16
17 import Outputable
18 import FastString
19 import SrcLoc
20
21 import Data.Data
22
23 {-
24 ************************************************************************
25 * *
26 \subsection{Import and export declaration lists}
27 * *
28 ************************************************************************
29
30 One per \tr{import} declaration in a module.
31 -}
32
33 type LImportDecl name = Located (ImportDecl name)
34 -- ^ When in a list this may have
35 --
36 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnSemi'
37 --
38
39 -- | A single Haskell @import@ declaration.
40 data ImportDecl name
41 = ImportDecl {
42 ideclName :: Located ModuleName, -- ^ Module name.
43 ideclPkgQual :: Maybe FastString, -- ^ Package qualifier.
44 ideclSource :: Bool, -- ^ True <=> {-\# SOURCE \#-} import
45 ideclSafe :: Bool, -- ^ True => safe import
46 ideclQualified :: Bool, -- ^ True => qualified
47 ideclImplicit :: Bool, -- ^ True => implicit import (of Prelude)
48 ideclAs :: Maybe ModuleName, -- ^ as Module
49 ideclHiding :: Maybe (Bool, Located [LIE name])
50 -- ^ (True => hiding, names)
51 }
52 -- ^
53 -- 'ApiAnnotation.AnnKeywordId's
54 --
55 -- - 'ApiAnnotation.AnnImport'
56 --
57 -- - 'ApiAnnotation.AnnOpen', 'ApiAnnotation.AnnClose' for ideclSource
58 --
59 -- - 'ApiAnnotation.AnnSafe','ApiAnnotation.AnnQualified',
60 -- 'ApiAnnotation.AnnPackageName','ApiAnnotation.AnnAs',
61 -- 'ApiAnnotation.AnnVal'
62 --
63 -- - 'ApiAnnotation.AnnHiding','ApiAnnotation.AnnOpen',
64 -- 'ApiAnnotation.AnnClose' attached
65 -- to location in ideclHiding
66
67 deriving (Data, Typeable)
68
69 simpleImportDecl :: ModuleName -> ImportDecl name
70 simpleImportDecl mn = ImportDecl {
71 ideclName = noLoc mn,
72 ideclPkgQual = Nothing,
73 ideclSource = False,
74 ideclSafe = False,
75 ideclImplicit = False,
76 ideclQualified = False,
77 ideclAs = Nothing,
78 ideclHiding = Nothing
79 }
80
81 instance (OutputableBndr name, HasOccName name) => Outputable (ImportDecl name) where
82 ppr (ImportDecl { ideclName = mod', ideclPkgQual = pkg
83 , ideclSource = from, ideclSafe = safe
84 , ideclQualified = qual, ideclImplicit = implicit
85 , ideclAs = as, ideclHiding = spec })
86 = hang (hsep [ptext (sLit "import"), ppr_imp from, pp_implicit implicit, pp_safe safe,
87 pp_qual qual, pp_pkg pkg, ppr mod', pp_as as])
88 4 (pp_spec spec)
89 where
90 pp_implicit False = empty
91 pp_implicit True = ptext (sLit ("(implicit)"))
92
93 pp_pkg Nothing = empty
94 pp_pkg (Just p) = doubleQuotes (ftext p)
95
96 pp_qual False = empty
97 pp_qual True = ptext (sLit "qualified")
98
99 pp_safe False = empty
100 pp_safe True = ptext (sLit "safe")
101
102 pp_as Nothing = empty
103 pp_as (Just a) = ptext (sLit "as") <+> ppr a
104
105 ppr_imp True = ptext (sLit "{-# SOURCE #-}")
106 ppr_imp False = empty
107
108 pp_spec Nothing = empty
109 pp_spec (Just (False, (L _ ies))) = ppr_ies ies
110 pp_spec (Just (True, (L _ ies))) = ptext (sLit "hiding") <+> ppr_ies ies
111
112 ppr_ies [] = ptext (sLit "()")
113 ppr_ies ies = char '(' <+> interpp'SP ies <+> char ')'
114
115 {-
116 ************************************************************************
117 * *
118 \subsection{Imported and exported entities}
119 * *
120 ************************************************************************
121 -}
122
123 type LIE name = Located (IE name)
124 -- ^ When in a list this may have
125 --
126 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma'
127 --
128
129 -- | Imported or exported entity.
130 data IE name
131 = IEVar (Located name)
132 -- ^ - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnPattern',
133 -- 'ApiAnnotation.AnnType'
134 | IEThingAbs name -- ^ Class/Type (can't tell)
135 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnPattern',
136 -- 'ApiAnnotation.AnnType','ApiAnnotation.AnnVal'
137 | IEThingAll (Located name) -- ^ Class/Type plus all methods/constructors
138 --
139 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen',
140 -- 'ApiAnnotation.AnnDotdot','ApiAnnotation.AnnClose',
141 -- 'ApiAnnotation.AnnType'
142
143 | IEThingWith (Located name) [Located name]
144 -- ^ Class/Type plus some methods/constructors
145 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnOpen',
146 -- 'ApiAnnotation.AnnClose',
147 -- 'ApiAnnotation.AnnComma',
148 -- 'ApiAnnotation.AnnType'
149 | IEModuleContents (Located ModuleName) -- ^ (Export Only)
150 --
151 -- - 'ApiAnnotation.AnnKeywordId's : 'ApiAnnotation.AnnModule'
152 | IEGroup Int HsDocString -- ^ Doc section heading
153 | IEDoc HsDocString -- ^ Some documentation
154 | IEDocNamed String -- ^ Reference to named doc
155 deriving (Eq, Data, Typeable)
156
157 ieName :: IE name -> name
158 ieName (IEVar (L _ n)) = n
159 ieName (IEThingAbs n) = n
160 ieName (IEThingWith (L _ n) _) = n
161 ieName (IEThingAll (L _ n)) = n
162 ieName _ = panic "ieName failed pattern match!"
163
164 ieNames :: IE a -> [a]
165 ieNames (IEVar (L _ n) ) = [n]
166 ieNames (IEThingAbs n ) = [n]
167 ieNames (IEThingAll (L _ n) ) = [n]
168 ieNames (IEThingWith (L _ n) ns) = n : map unLoc ns
169 ieNames (IEModuleContents _ ) = []
170 ieNames (IEGroup _ _ ) = []
171 ieNames (IEDoc _ ) = []
172 ieNames (IEDocNamed _ ) = []
173
174 pprImpExp :: (HasOccName name, OutputableBndr name) => name -> SDoc
175 pprImpExp name = type_pref <+> pprPrefixOcc name
176 where
177 occ = occName name
178 type_pref | isTcOcc occ && isSymOcc occ = ptext (sLit "type")
179 | otherwise = empty
180
181 instance (HasOccName name, OutputableBndr name) => Outputable (IE name) where
182 ppr (IEVar var) = pprPrefixOcc (unLoc var)
183 ppr (IEThingAbs thing) = pprImpExp thing
184 ppr (IEThingAll thing) = hcat [pprImpExp (unLoc thing), text "(..)"]
185 ppr (IEThingWith thing withs)
186 = pprImpExp (unLoc thing) <> parens (fsep (punctuate comma
187 (map pprImpExp $ map unLoc withs)))
188 ppr (IEModuleContents mod')
189 = ptext (sLit "module") <+> ppr mod'
190 ppr (IEGroup n _) = text ("<IEGroup: " ++ (show n) ++ ">")
191 ppr (IEDoc doc) = ppr doc
192 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")