2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 HsImpExp: Abstract syntax: imports, exports, interfaces
11 #include "HsVersions.h"
13 import Module ( ModuleName )
14 import HsDoc ( HsDoc )
18 import SrcLoc ( Located(..) )
19 import Char ( isAlpha )
22 %************************************************************************
24 \subsection{Import and export declaration lists}
26 %************************************************************************
28 One per \tr{import} declaration in a module.
30 type LImportDecl name = Located (ImportDecl name)
33 = ImportDecl (Located ModuleName) -- module name
34 Bool -- True <=> {-# SOURCE #-} import
35 Bool -- True => qualified
36 (Maybe ModuleName) -- as Module
37 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
41 instance (Outputable name) => Outputable (ImportDecl name) where
42 ppr (ImportDecl mod from qual as spec)
43 = hang (hsep [ptext SLIT("import"), ppr_imp from,
44 pp_qual qual, ppr mod, pp_as as])
48 pp_qual True = ptext SLIT("qualified")
51 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
53 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
56 pp_spec Nothing = empty
57 pp_spec (Just (False, spec))
58 = parens (interpp'SP spec)
59 pp_spec (Just (True, spec))
60 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
62 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
65 %************************************************************************
67 \subsection{Imported and exported entities}
69 %************************************************************************
72 type LIE name = Located (IE name)
76 | IEThingAbs name -- Class/Type (can't tell)
77 | IEThingAll name -- Class/Type plus all methods/constructors
78 | IEThingWith name [name] -- Class/Type plus some methods/constructors
79 | IEModuleContents ModuleName -- (Export Only)
80 | IEGroup Int (HsDoc name) -- Doc section heading
81 | IEDoc (HsDoc name) -- Some documentation
82 | IEDocNamed String -- Reference to named doc
86 ieName :: IE name -> name
88 ieName (IEThingAbs n) = n
89 ieName (IEThingWith n _) = n
90 ieName (IEThingAll n) = n
92 ieNames :: IE a -> [a]
93 ieNames (IEVar n ) = [n]
94 ieNames (IEThingAbs n ) = [n]
95 ieNames (IEThingAll n ) = [n]
96 ieNames (IEThingWith n ns) = n:ns
97 ieNames (IEModuleContents _ ) = []
98 ieNames (IEGroup _ _ ) = []
99 ieNames (IEDoc _ ) = []
100 ieNames (IEDocNamed _ ) = []
104 instance (Outputable name) => Outputable (IE name) where
105 ppr (IEVar var) = pprHsVar var
106 ppr (IEThingAbs thing) = ppr thing
107 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
108 ppr (IEThingWith thing withs)
109 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
110 ppr (IEModuleContents mod)
111 = ptext SLIT("module") <+> ppr mod
112 ppr (IEGroup n doc) = text ("<IEGroup: " ++ (show n) ++ ">")
113 ppr (IEDoc doc) = ppr doc
114 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")
118 pprHsVar :: Outputable name => name -> SDoc
119 pprHsVar v | isOperator ppr_v = parens ppr_v
124 isOperator :: SDoc -> Bool
126 = case showSDocUnqual ppr_v of
127 ('(':s) -> False -- (), (,) etc
128 ('[':s) -> False -- []
129 ('$':c:s) -> not (isAlpha c) -- Don't treat $d as an operator
130 (':':c:s) -> not (isAlpha c) -- Don't treat :T as an operator
131 ('_':s) -> False -- Not an operator
132 (c:s) -> not (isAlpha c) -- Starts with non-alpha
134 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
135 -- that we don't need NamedThing in the context of all these functions.
136 -- Gruesome, but simple.