2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsImpExp]{Abstract syntax: imports, exports, interfaces}
9 #include "HsVersions.h"
11 import Module ( ModuleName )
14 import SrcLoc ( SrcLoc )
15 import Char ( isAlpha )
18 %************************************************************************
20 \subsection{Import and export declaration lists}
22 %************************************************************************
24 One per \tr{import} declaration in a module.
27 = ImportDecl ModuleName -- module name
28 Bool -- True <=> {-# SOURCE #-} import
29 Bool -- True => qualified
30 (Maybe ModuleName) -- as Module
31 (Maybe (Bool, [IE name])) -- (True => hiding, names)
36 instance (Outputable name) => Outputable (ImportDecl name) where
37 ppr (ImportDecl mod from qual as spec _)
38 = hang (hsep [ptext SLIT("import"), ppr_imp from,
39 pp_qual qual, ppr mod, pp_as as])
43 pp_qual True = ptext SLIT("qualified")
46 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
48 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
51 pp_spec Nothing = empty
52 pp_spec (Just (False, spec))
53 = parens (interpp'SP spec)
54 pp_spec (Just (True, spec))
55 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
57 ideclName (ImportDecl mod_nm _ _ _ _ _) = mod_nm
60 %************************************************************************
62 \subsection{Imported and exported entities}
64 %************************************************************************
69 | IEThingAbs name -- Class/Type (can't tell)
70 | IEThingAll name -- Class/Type plus all methods/constructors
71 | IEThingWith name [name] -- Class/Type plus some methods/constructors
72 | IEModuleContents ModuleName -- (Export Only)
76 ieName :: IE name -> name
78 ieName (IEThingAbs n) = n
79 ieName (IEThingWith n _) = n
80 ieName (IEThingAll n) = n
82 ieNames :: IE a -> [a]
83 ieNames (IEVar n ) = [n]
84 ieNames (IEThingAbs n ) = [n]
85 ieNames (IEThingAll n ) = [n]
86 ieNames (IEThingWith n ns) = n:ns
87 ieNames (IEModuleContents _ ) = []
91 instance (Outputable name) => Outputable (IE name) where
92 ppr (IEVar var) = pprHsVar var
93 ppr (IEThingAbs thing) = ppr thing
94 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
95 ppr (IEThingWith thing withs)
96 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
97 ppr (IEModuleContents mod)
98 = ptext SLIT("module") <+> ppr mod
102 pprHsVar :: Outputable name => name -> SDoc
103 pprHsVar v | isOperator ppr_v = parens ppr_v
108 isOperator :: SDoc -> Bool
110 = case showSDocUnqual ppr_v of
111 ('(':s) -> False -- (), (,) etc
112 ('[':s) -> False -- []
113 ('$':c:s) -> not (isAlpha c) -- Don't treat $d as an operator
114 (':':c:s) -> not (isAlpha c) -- Don't treat :T as an operator
115 (c:s) -> not (isAlpha c) -- Starts with non-alpha
117 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
118 -- that we don't need NamedThing in the context of all these functions.
119 -- Gruesome, but simple.