2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 HsImpExp: Abstract syntax: imports, exports, interfaces
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
18 #include "HsVersions.h"
20 import Module ( ModuleName )
21 import HsDoc ( HsDoc )
25 import SrcLoc ( Located(..) )
26 import Char ( isAlpha )
29 %************************************************************************
31 \subsection{Import and export declaration lists}
33 %************************************************************************
35 One per \tr{import} declaration in a module.
37 type LImportDecl name = Located (ImportDecl name)
40 = ImportDecl (Located ModuleName) -- module name
41 Bool -- True <=> {-# SOURCE #-} import
42 Bool -- True => qualified
43 (Maybe ModuleName) -- as Module
44 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
48 instance (Outputable name) => Outputable (ImportDecl name) where
49 ppr (ImportDecl mod from qual as spec)
50 = hang (hsep [ptext SLIT("import"), ppr_imp from,
51 pp_qual qual, ppr mod, pp_as as])
55 pp_qual True = ptext SLIT("qualified")
58 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
60 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
63 pp_spec Nothing = empty
64 pp_spec (Just (False, spec))
65 = parens (interpp'SP spec)
66 pp_spec (Just (True, spec))
67 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
69 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
72 %************************************************************************
74 \subsection{Imported and exported entities}
76 %************************************************************************
79 type LIE name = Located (IE name)
83 | IEThingAbs name -- Class/Type (can't tell)
84 | IEThingAll name -- Class/Type plus all methods/constructors
85 | IEThingWith name [name] -- Class/Type plus some methods/constructors
86 | IEModuleContents ModuleName -- (Export Only)
87 | IEGroup Int (HsDoc name) -- Doc section heading
88 | IEDoc (HsDoc name) -- Some documentation
89 | IEDocNamed String -- Reference to named doc
93 ieName :: IE name -> name
95 ieName (IEThingAbs n) = n
96 ieName (IEThingWith n _) = n
97 ieName (IEThingAll n) = n
99 ieNames :: IE a -> [a]
100 ieNames (IEVar n ) = [n]
101 ieNames (IEThingAbs n ) = [n]
102 ieNames (IEThingAll n ) = [n]
103 ieNames (IEThingWith n ns) = n:ns
104 ieNames (IEModuleContents _ ) = []
105 ieNames (IEGroup _ _ ) = []
106 ieNames (IEDoc _ ) = []
107 ieNames (IEDocNamed _ ) = []
111 instance (Outputable name) => Outputable (IE name) where
112 ppr (IEVar var) = pprHsVar var
113 ppr (IEThingAbs thing) = ppr thing
114 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
115 ppr (IEThingWith thing withs)
116 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
117 ppr (IEModuleContents mod)
118 = ptext SLIT("module") <+> ppr mod
119 ppr (IEGroup n doc) = text ("<IEGroup: " ++ (show n) ++ ">")
120 ppr (IEDoc doc) = ppr doc
121 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")
125 pprHsVar :: Outputable name => name -> SDoc
126 pprHsVar v | isOperator ppr_v = parens ppr_v
131 isOperator :: SDoc -> Bool
133 = case showSDocUnqual ppr_v of
134 ('(':s) -> False -- (), (,) etc
135 ('[':s) -> False -- []
136 ('$':c:s) -> not (isAlpha c) -- Don't treat $d as an operator
137 (':':c:s) -> not (isAlpha c) -- Don't treat :T as an operator
138 ('_':s) -> False -- Not an operator
139 (c:s) -> not (isAlpha c) -- Starts with non-alpha
141 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
142 -- that we don't need NamedThing in the context of all these functions.
143 -- Gruesome, but simple.