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 ( Located(..) )
15 import Char ( isAlpha )
18 %************************************************************************
20 \subsection{Import and export declaration lists}
22 %************************************************************************
24 One per \tr{import} declaration in a module.
26 type LImportDecl name = Located (ImportDecl name)
29 = ImportDecl (Located ModuleName) -- module name
30 Bool -- True <=> {-# SOURCE #-} import
31 Bool -- True => qualified
32 (Maybe ModuleName) -- as Module
33 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
37 instance (Outputable name) => Outputable (ImportDecl name) where
38 ppr (ImportDecl mod from qual as spec)
39 = hang (hsep [ptext SLIT("import"), ppr_imp from,
40 pp_qual qual, ppr mod, pp_as as])
44 pp_qual True = ptext SLIT("qualified")
47 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
49 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
52 pp_spec Nothing = empty
53 pp_spec (Just (False, spec))
54 = parens (interpp'SP spec)
55 pp_spec (Just (True, spec))
56 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
58 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
61 %************************************************************************
63 \subsection{Imported and exported entities}
65 %************************************************************************
68 type LIE name = Located (IE name)
72 | IEThingAbs name -- Class/Type (can't tell)
73 | IEThingAll name -- Class/Type plus all methods/constructors
74 | IEThingWith name [name] -- Class/Type plus some methods/constructors
75 | IEModuleContents ModuleName -- (Export Only)
79 ieName :: IE name -> name
81 ieName (IEThingAbs n) = n
82 ieName (IEThingWith n _) = n
83 ieName (IEThingAll n) = n
85 ieNames :: IE a -> [a]
86 ieNames (IEVar n ) = [n]
87 ieNames (IEThingAbs n ) = [n]
88 ieNames (IEThingAll n ) = [n]
89 ieNames (IEThingWith n ns) = n:ns
90 ieNames (IEModuleContents _ ) = []
94 instance (Outputable name) => Outputable (IE name) where
95 ppr (IEVar var) = pprHsVar var
96 ppr (IEThingAbs thing) = ppr thing
97 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
98 ppr (IEThingWith thing withs)
99 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
100 ppr (IEModuleContents mod)
101 = ptext SLIT("module") <+> ppr mod
105 pprHsVar :: Outputable name => name -> SDoc
106 pprHsVar v | isOperator ppr_v = parens ppr_v
111 isOperator :: SDoc -> Bool
113 = case showSDocUnqual ppr_v of
114 ('(':s) -> False -- (), (,) etc
115 ('[':s) -> False -- []
116 ('$':c:s) -> not (isAlpha c) -- Don't treat $d as an operator
117 (':':c:s) -> not (isAlpha c) -- Don't treat :T as an operator
118 ('_':s) -> False -- Not an operator
119 (c:s) -> not (isAlpha c) -- Starts with non-alpha
121 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
122 -- that we don't need NamedThing in the context of all these functions.
123 -- Gruesome, but simple.