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 )
12 import HsDoc ( HsDoc )
16 import SrcLoc ( Located(..) )
17 import Char ( isAlpha )
20 %************************************************************************
22 \subsection{Import and export declaration lists}
24 %************************************************************************
26 One per \tr{import} declaration in a module.
28 type LImportDecl name = Located (ImportDecl name)
31 = ImportDecl (Located ModuleName) -- module name
32 Bool -- True <=> {-# SOURCE #-} import
33 Bool -- True => qualified
34 (Maybe ModuleName) -- as Module
35 (Maybe (Bool, [LIE name])) -- (True => hiding, names)
39 instance (Outputable name) => Outputable (ImportDecl name) where
40 ppr (ImportDecl mod from qual as spec)
41 = hang (hsep [ptext SLIT("import"), ppr_imp from,
42 pp_qual qual, ppr mod, pp_as as])
46 pp_qual True = ptext SLIT("qualified")
49 pp_as (Just a) = ptext SLIT("as ") <+> ppr a
51 ppr_imp True = ptext SLIT("{-# SOURCE #-}")
54 pp_spec Nothing = empty
55 pp_spec (Just (False, spec))
56 = parens (interpp'SP spec)
57 pp_spec (Just (True, spec))
58 = ptext SLIT("hiding") <+> parens (interpp'SP spec)
60 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
63 %************************************************************************
65 \subsection{Imported and exported entities}
67 %************************************************************************
70 type LIE name = Located (IE name)
74 | IEThingAbs name -- Class/Type (can't tell)
75 | IEThingAll name -- Class/Type plus all methods/constructors
76 | IEThingWith name [name] -- Class/Type plus some methods/constructors
77 | IEModuleContents ModuleName -- (Export Only)
78 | IEGroup Int (HsDoc name) -- Doc section heading
79 | IEDoc (HsDoc name) -- Some documentation
80 | IEDocNamed String -- Reference to named doc
84 ieName :: IE name -> name
86 ieName (IEThingAbs n) = n
87 ieName (IEThingWith n _) = n
88 ieName (IEThingAll n) = n
90 ieNames :: IE a -> [a]
91 ieNames (IEVar n ) = [n]
92 ieNames (IEThingAbs n ) = [n]
93 ieNames (IEThingAll n ) = [n]
94 ieNames (IEThingWith n ns) = n:ns
95 ieNames (IEModuleContents _ ) = []
96 ieNames (IEGroup _ _ ) = []
97 ieNames (IEDoc _ ) = []
98 ieNames (IEDocNamed _ ) = []
102 instance (Outputable name) => Outputable (IE name) where
103 ppr (IEVar var) = pprHsVar var
104 ppr (IEThingAbs thing) = ppr thing
105 ppr (IEThingAll thing) = hcat [ppr thing, text "(..)"]
106 ppr (IEThingWith thing withs)
107 = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
108 ppr (IEModuleContents mod)
109 = ptext SLIT("module") <+> ppr mod
110 ppr (IEGroup n doc) = text ("<IEGroup: " ++ (show n) ++ ">")
111 ppr (IEDoc doc) = ppr doc
112 ppr (IEDocNamed string) = text ("<IEDocNamed: " ++ string ++ ">")
116 pprHsVar :: Outputable name => name -> SDoc
117 pprHsVar v | isOperator ppr_v = parens ppr_v
122 isOperator :: SDoc -> Bool
124 = case showSDocUnqual ppr_v of
125 ('(':s) -> False -- (), (,) etc
126 ('[':s) -> False -- []
127 ('$':c:s) -> not (isAlpha c) -- Don't treat $d as an operator
128 (':':c:s) -> not (isAlpha c) -- Don't treat :T as an operator
129 ('_':s) -> False -- Not an operator
130 (c:s) -> not (isAlpha c) -- Starts with non-alpha
132 -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
133 -- that we don't need NamedThing in the context of all these functions.
134 -- Gruesome, but simple.